public DictionaryImport() { InitializeComponent(); select.SelectedIndexChanged += new EventHandler(select_SelectedIndexChanged); this.Closed += new EventHandler(DictionaryImport_Closed); select.BeginUpdate(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { string name = null; string description = type.Name; Attribute[] attributes = Attribute.GetCustomAttributes(type); foreach (Attribute attribute in attributes) { if (attribute is ImporterAttribute && ((ImporterAttribute)attribute).Type == typeof(IBilingualDictionary)) { name = ((ImporterAttribute)attribute).Name; } else if (attribute is ImporterAttribute && ((ImporterAttribute)attribute).Type == typeof(IDictionarySection)) { name = ((ImporterAttribute)attribute).Name; } else if (attribute is ImporterDescriptionAttribute) { description = Resources.ImporterDescriptions.ResourceManager.GetString( ((ImporterDescriptionAttribute)attribute).ResourceIdentifier, CultureInfo.CurrentUICulture) ?? description; } } if (name != null) { ImporterItem item = new ImporterItem(type, name, description); // Not to be vain, or anything, but I think this should be the default choice of importer. if (type == typeof(Importing.DualSectionImporter)) { select.Items.Insert(0, item); } else { select.Items.Add(item); } } } } select.EndUpdate(); if (select.Items.Count > 0) { select.SelectedIndex = 0; } }
void select_SelectedIndexChanged(object sender, EventArgs e) { ImporterItem item = (select.SelectedItem as ImporterItem); if (item != null) { if (importer != null) { UnwireImporterEvents(); importer.Dispose(); } IImporterUI <IBilingualDictionary> newUI = null; if (item.Type.GetInterfaces().Contains(typeof(IDictionarySectionImporter))) { var si = (IDictionarySectionImporter)item.Type.GetConstructor(new Type[] { }).Invoke(new object[] { }); var dsi = new DualSectionImporter(); newUI = new Controls.DictionarySectionUI(dsi, si); importer = dsi; } else if (item.Type.IsSubclassOf(typeof(IBilingualDictionary))) { importer = (IImporter <IBilingualDictionary>)item.Type.GetConstructor(new Type[] { }).Invoke(new object[] { }); foreach (Type t in importer.GetType().Assembly.GetTypes()) { var attr = Attribute.GetCustomAttribute(t, typeof(ImporterUIAttribute)) as ImporterUIAttribute; if (attr != null) { newUI = (IImporterUI <IBilingualDictionary>)Activator.CreateInstance(t, importer); break; } } } WireImporterEvents(); if (newUI == null) { CurrentUI = new Controls.ErrorUI("No UI for " + item.Name, ""); UnwireImporterEvents(); importer.Dispose(); importer = null; return; } newUI.Finished += new EventHandler(this.ImporterUIFinished); CurrentUI = (Control)newUI; } }
public DictionaryImport() { InitializeComponent(); select.SelectedIndexChanged += new EventHandler(select_SelectedIndexChanged); this.Closed += new EventHandler(DictionaryImport_Closed); select.BeginUpdate(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { string name = null; string description = type.Name; Attribute[] attributes = Attribute.GetCustomAttributes(type); foreach (Attribute attribute in attributes) { if (attribute is ImporterAttribute && ((ImporterAttribute)attribute).Type == typeof(IBilingualDictionary)) name = ((ImporterAttribute)attribute).Name; else if (attribute is ImporterAttribute && ((ImporterAttribute)attribute).Type == typeof(IDictionarySection)) name = ((ImporterAttribute)attribute).Name; else if (attribute is ImporterDescriptionAttribute) description = Resources.ImporterDescriptions.ResourceManager.GetString( ((ImporterDescriptionAttribute)attribute).ResourceIdentifier, CultureInfo.CurrentUICulture) ?? description; } if (name != null) { ImporterItem item = new ImporterItem(type, name, description); // Not to be vain, or anything, but I think this should be the default choice of importer. if (type == typeof(Importing.DualSectionImporter)) { select.Items.Insert(0, item); } else { select.Items.Add(item); } } } } select.EndUpdate(); if (select.Items.Count > 0) select.SelectedIndex = 0; }