public static void CreateTool(int settingKey, ToolReflection toolReflection) { object[] parameters = new object[1]; parameters[0] = settingKey; ToolBase tool = CreateInstance <ToolBase>(toolReflection, parameters); if (tool == null) { throw new Exception("工具创建失败"); //整个程序中断,也用不着return. } int index = 0; int name = 1; //GetToolList(settingKey); //当ToolsDic.ContainsKey(settingKey)为false时,下面的GetToolList(settingKey)已经生成,本处没有任何意义。 string nameTmp = "";/* = string.Format("C{0}{1:00}", settingKey, name)*/ //index = GetToolList(settingKey).FindIndex(x => x.Name == nameTmp); while (index != -1) { nameTmp = string.Format("C{0}{1:00}", settingKey, name); index = GetToolList(settingKey).FindIndex(x => x.Name == nameTmp); //如果这时GetToolList(settingKey)元素为空,则index = -1; name++; } tool.SetToolName(nameTmp); //把工具名字赋给相应工具。 if (tool != null) { GetToolList(settingKey).Add(tool); } }
private static T CreateInstance <T>(ToolReflection toolReflection, object[] parameters) { try { //toolReflection.Type.FullName, Type事实上就是类的名字及全名(完全路径+名字) object ect = toolReflection.Assembly.CreateInstance(toolReflection.Type.FullName, true, System.Reflection.BindingFlags.Default, null, parameters /*如果该参数为空,创建该实例时就会调用默认的不带参数构造函数*/, null, null); //加载程序集,创建该程序集里面的命名空间.类型名 实例 return((T)ect); //类型转换并返回 } catch (Exception ex) { Util.WriteLog(typeof(ToolsFactory), ex); //发生异常,返回类型的默认值 return(default(T)); } }