готовая 4 лаба

This commit is contained in:
Полина Чубыкина 2024-11-12 08:59:29 +04:00
parent d17275873b
commit 3c0d1f9d6a
3 changed files with 38 additions and 54 deletions

View File

@ -23,10 +23,4 @@
<ProjectReference Include="..\PluginsConventionLibrary\PluginsConventionLibrary.csproj" /> <ProjectReference Include="..\PluginsConventionLibrary\PluginsConventionLibrary.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="System.IO.Packaging">
<HintPath>$(SolutionDir)Plugins\System.IO.Packaging.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

View File

@ -20,58 +20,43 @@ namespace AppByPlugins
public FormMain() public FormMain()
{ {
InitializeComponent(); InitializeComponent();
_plugins = new(); _plugins = LoadPlugins();
LoadPlugins();
_selectedPlugin = string.Empty; _selectedPlugin = string.Empty;
} }
private void LoadPlugins() private Dictionary<string, IPluginsConvention> LoadPlugins()
{ {
List<IPluginsConvention> pluginsList = GetPlugins(); var plugins = new Dictionary<string, IPluginsConvention>();
foreach (var plugin in pluginsList) string pluginsDir = Directory.GetParent(Directory.GetCurrentDirectory())!.Parent!.Parent!.Parent!.FullName + "\\Plugins";
{
_plugins[plugin.PluginName] = plugin;
CreateMenuItem(plugin.PluginName);
}
}
private List<IPluginsConvention> GetPlugins() string[] dllFiles = Directory.GetFiles(pluginsDir, "*.dll", SearchOption.AllDirectories);
{
string currentDir = Environment.CurrentDirectory; foreach (string dllFile in dllFiles)
string pluginsDir = Directory.GetParent(currentDir).Parent.Parent.Parent.FullName + "\\Plugins"; {
string[] dllFiles = Directory.GetFiles( try
pluginsDir, {
"*.dll", Assembly assembly = Assembly.LoadFrom(dllFile);
SearchOption.AllDirectories Type[] types = assembly.GetTypes();
);
List<IPluginsConvention> plugins = new(); foreach (var type in types)
foreach (string dllFile in dllFiles) {
{ if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface)
try {
{ var plugin = (IPluginsConvention)Activator.CreateInstance(type)!;
Assembly assembly = Assembly.LoadFrom(dllFile); plugins.Add(plugin.PluginName, plugin);
Type[] types = assembly.GetTypes(); CreateMenuItem(plugin.PluginName);
foreach (Type type in types) }
{ }
if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface) }
{ catch (Exception ex)
if (Activator.CreateInstance(type) is IPluginsConvention plugin) {
{ Console.WriteLine($"Îøèáêà ïðè çàãðóçêå ñáîðêè {dllFile}: {ex.Message}");
plugins.Add(plugin); }
} }
}
} return plugins;
} }
catch (Exception ex)
{
MessageBox.Show(
ex.Message
);
}
}
return plugins;
}
private void CreateMenuItem(string pluginName) private void CreateMenuItem(string pluginName)
{ {

View File

@ -25,7 +25,11 @@ namespace Components.Components
} }
set set
{ {
if (value != null && checkedListBox.Items.Contains(value)) checkedListBox.SelectedItem = value; if (value != null && checkedListBox.Items.Contains(value))
{
checkedListBox.SelectedItem = value;
checkedListBox.SetItemChecked(checkedListBox.SelectedIndex, true);
}
} }
} }
@ -53,6 +57,7 @@ namespace Components.Components
if (index != checkedListBox.SelectedIndex) if (index != checkedListBox.SelectedIndex)
{ {
checkedListBox.SetItemChecked(index, false); checkedListBox.SetItemChecked(index, false);
checkedListBox.SetItemChecked(checkedListBox.SelectedIndex, false);
} }
} }
} }