private void EnumImportersFromDirectory(string importerDirectory) { if (!Directory.Exists(importerDirectory)) { return; } string[] Files = Directory.GetFiles(importerDirectory, "*.DLL"); List <string> OrderedFiles = OrderedImporterFiles(Files); foreach (string file in OrderedFiles) { //we know this is a native image MainForm.LogMessage("detecting importer file " + file + " version and creation date " + FileVersions(file)); if (Path.GetFileName(file).ToUpper() == "BulkLoad.dll".ToUpper()) { MainForm.LogMessage(String.Format(Properties.Resources.Msg_NativeImage, file)); continue; } Assembly Assem; try { Assem = Assembly.LoadFile(file); } catch (Exception ex) { MainForm.LogMessage("Assembly " + file + " could not be used as an importer: " + ex.Message, MessageOptions.Silent); continue; } Type[] typs = Assem.GetExportedTypes(); foreach (Type typ in typs) { //Ignore abstract classes if (typ.IsAbstract) { continue; } //Ignore non-classes if (!typ.IsClass) { continue; } TypeFilter loadIfcFilt = new TypeFilter(loadIfcFilter); Type[] ifcs = typ.FindInterfaces(loadIfcFilt, "NexusInterfaces.INexusImporter"); foreach (Type ifc in ifcs) { //If we get in here, the Class implements the interface, so add it to the list //and bail INexusImporter prod = (INexusImporter)Assem.CreateInstance(typ.FullName, true); prod.StatusChanged += new System.EventHandler(this.ImportStatusChanged); if (prod is INexusProgressReporter) { (prod as INexusProgressReporter).ProgressChanged += new System.EventHandler(this.ImportProgressChanged); } ToolStripMenuItem tsi = new ToolStripMenuItem(prod.Name); tsi.Tag = prod; tsiImporters.DropDownItems.Add(tsi); // Add subitems // Enabled item // All importers require an "Enabled" menu option -- if this doesn't exist in the options collection, add it. object enabledoption; if (!prod.Options.TryGetValue("Enabled", out enabledoption)) { prod.Options.Add("Enabled", true); } // options ToolStripMenuItem subtsi; foreach (string option in prod.Options.Keys) { subtsi = new ToolStripMenuItem(option); if (option.Substring(option.Length - 3) == "...") // dialog { subtsi.Tag = prod.OptionsDialog; subtsi.Click += new System.EventHandler(this.tsiDialog_Click); } else // boolean { m_OptionList.Add(subtsi); subtsi.Tag = prod; subtsi.CheckOnClick = true; bool UserSaved = ImportOptions.IsEnabled(String.Format("{0}.{1}", prod.Name, subtsi.Text)); MainForm.LogMessage("load: " + String.Format("{0}.{1}", prod.Name, option), MessageOptions.Silent); if (ImportOptions.IsEnabled("SaveImportOptions")) { subtsi.Checked = UserSaved; } else { subtsi.Checked = (bool)prod.Options[option]; } subtsi.Click += new System.EventHandler(this.tsiBool_Click); } tsi.DropDownItems.Add(subtsi); } } } } }
/// <summary> /// 把DataGrid中的数据按行赋给业务对象 /// </summary> /// <param name="grid">DataGrid实例</param> /// <param name="model">业务对象的类型</param> /// <param name="isThrow">是否抛出异常</param> /// <returns>业务对象的集合</returns> public static List <object> BindModelByDataGrid(DataGrid grid, System.Type model, bool isThrow) { int RowsCount = -1; List <object> list; Assembly Assem; RowsCount = grid.Items.Count; //动态创建业务对象集合。 try { Assem = Assembly.Load(model.Assembly.FullName); list = new List <object>(); for (int i = 0; i < RowsCount; i++) { list.Add(Assem.CreateInstance(model.FullName, true)); } } catch { return(null); } //获取业务类公共实例属性 PropertyInfo[] Properties = model.GetProperties(BindingFlags.Instance | BindingFlags.Public); #region 遍历每一列赋值 for (int i = 0; i < grid.Columns.Count; i++) { if (grid.Columns[i] is BoundColumn) { BoundColumn BC = grid.Columns[i] as BoundColumn; foreach (PropertyInfo Property in Properties) { if (Property.Name == BC.DataField) { //把该列的每一行赋值给业务对象 for (int j = 0; j < RowsCount; j++) { try { if (grid.Items[j].Cells[i].HasControls()) { Property.SetValue(list[j], Convert.ChangeType((grid.Items[j].Cells[i].Controls[0] as TextBox).Text, Property.PropertyType), null); } else { Property.SetValue(list[j], Convert.ChangeType(grid.Items[j].Cells[i].Text, Property.PropertyType), null); } } catch { if (isThrow) { throw; } else { continue; } } } } } } } #endregion 遍历每一列赋值 return(list); }