private void LoadAddIns() { string iColumnProviderName = typeof(IColumnProvider).FullName; string iContextMenuItemProviderName = typeof(IContextMenuItemProvider).FullName; DirectoryInfo addInDirInfo = new DirectoryInfo(Path.Combine(Application.StartupPath, "AddIns")); if (addInDirInfo.Exists) { // Get all of the assemblies in the AddIns directory foreach (FileInfo fileInfo in addInDirInfo.GetFiles("*.dll")) { // Load the assembly and iterate through all of the types in it. Assembly assembly = Assembly.LoadFrom(fileInfo.FullName); foreach (Type type in assembly.GetTypes()) { // If the type implements IColumnProvider, create an instance of it and add the column it represents to the list view. if (type.GetInterface(iColumnProviderName) != null) { IColumnProvider columnProvider = (IColumnProvider)Activator.CreateInstance(type); Program.ColumnProviders.Add(columnProvider); ColumnHeader columnHeader = listView.Columns.Add(columnProvider.HeaderText, columnProvider.DefaultWidth, columnProvider.Alignment); columnProvider.Initialize(columnHeader.Index); } // If the type implements IContextMenuItemProvider, create an instance of it and add it to the context menu. if (type.GetInterface(iContextMenuItemProviderName) != null) { IContextMenuItemProvider menuItemProvider = (IContextMenuItemProvider)Activator.CreateInstance(type); Program.ContextMenuItemProviders.Add(menuItemProvider); MenuItemProviderMenuItem contextMenuItem = new MenuItemProviderMenuItem(menuItemProvider); contextMenuItem.Click += contextMenuItem_Click; contextMenu.Items.Add(contextMenuItem); } } } } }
public MenuItemProviderMenuItem(IContextMenuItemProvider menuItemProvider) : base(menuItemProvider.Text) { _menuItemProvider = menuItemProvider; }