public static T CreateFrom <T>(TypeConfigurationElement typeConfiguration) { var constructorParameterElements = typeConfiguration.ConstructorParameters.ToList(); var parameters = new object[] { }; if (constructorParameterElements.Count > 0) { var tmp = new List <object>(); constructorParameterElements.ForEach(c => tmp.Add(c.Value)); parameters = tmp.ToArray(); } var type = Type.GetType(typeConfiguration.Type); if (type == null) { throw new Exception(string.Format("'{0}' cannot convert to type. ", typeConfiguration.Type)); } var constructorInfos = type.GetConstructors().Where(c => c.GetParameters().Count() == parameters.Count()); var constructor = constructorInfos.FirstOrDefault(c => c.GetParameters().Select(p => p.Name).All( p => constructorParameterElements.Select(c2 => c2.Name). Contains(p))); if (constructor == null) { throw new Exception("Constructor yakalanamadı!"); } var instance = constructor.Invoke(parameters); return((T)instance); }
private void CreateTabControl(TypeConfigurationCollection configs, SqlExerciseItem item, TabPage tab) { tab.Controls.Clear(); try { if (configs.ContainsKey(item.Name)) { TypeConfigurationElement typeElement = configs[item.Name]; UserControl uc = typeElement.CreateInstance() as UserControl; if (uc != null) { tab.Controls.Clear(); uc.Dock = DockStyle.Fill; tab.Controls.Add(uc); } else { tabControl1.TabPages.Remove(tab); } } } catch (Exception ex) { MessageBox.Show(string.Format("加载扩展配置控件失败,异常信息:{0}", ex.Message)); //移除扩展配置项 tabControl1.TabPages.Remove(tab); } }
/// <summary> /// 得到执行器的工厂类 /// </summary> /// <returns></returns> public IArchiveExecutorFactory GetFactory() { TypeConfigurationElement elem = Factories["archiveExecutor"]; ExceptionHelper.FalseThrow(elem != null, "没有在配置节archiveSettings中配置archiveExecutor的插件"); return((IArchiveExecutorFactory)elem.CreateInstance()); }
public IWfAction GetAction(string actionName) { TypeConfigurationElement actionConfig = Actions[actionName]; (actionConfig != null).FalseThrow <SettingsPropertyNotFoundException>("不能在actionSettings中找到名称为{0}的Action", actionName); return(actionConfig.CreateInstance() as IWfAction); }
private T GetTypeInstance <T>(string factoryName) { factoryName.CheckStringIsNullOrEmpty("factoryName"); TypeConfigurationElement factoryElement = (TypeConfigurationElement)TypeFactories[factoryName]; factoryElement.NullCheck <SystemSupportException>("不能在配置节workflowSettings/typeFactories找到{0}的配置项", factoryName); return((T)factoryElement.CreateInstance()); }
/// <summary> /// 找到指定key的类型配置,并且创建指定类型的实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="autoThrow">是否自动抛出异常</param> /// <returns></returns> public T CheckAndGetInstance <T>(string key, bool autoThrow = true) where T : class { TypeConfigurationElement element = this.CheckAndGet(key, autoThrow); T instance = default(T); if (element != null) { instance = (T)element.CreateInstance(); } return(instance); }
public TabPageTag(TypeConfigurationElement desp) { this.controlDesp = desp; }