/// <summary> /// 从配置文件中读取数据 /// </summary> /// <param name="doc">配置文件</param> /// <returns>配置数据</returns> private static InjectionModel GetDataFromXml(XmlDocument doc) { var model = new InjectionModel(); foreach (XmlNode node in doc.DocumentElement.SelectSingleNode("modules").ChildNodes) { if (!(node is XmlComment)) { var module = new InjectionModuleModel { Name = node.Attributes["name"]?.Value, Assembly = node.Attributes["assembly"]?.Value, Class = node.Attributes["class"]?.Value }; if (string.IsNullOrWhiteSpace(module.Name)) { //如果名称为空, 随便起一个名 module.Name = Guid.NewGuid().ToString(); } foreach (XmlNode p_node in node.ChildNodes) { if (!(p_node is XmlComment)) { var parameter = new InjectionParameterModel { Name = p_node.Attributes["name"]?.Value, Ref = p_node.Attributes["ref"]?.Value, Value = p_node.Attributes["value"]?.Value, }; switch (p_node.Name) { case "constructor": //构造参数 module.ConstructorList.Add(parameter); break; case "property": //待注入属性 module.PropertyList.Add(parameter); break; default: break; } } } model.Modules.Add(module); } } return(model); }
/// <summary> /// 实例化模块集合 /// </summary> /// <param name="model">模块集合信息</param> /// <returns>实例化模块对象集合</returns> private static InjectionModel InstantiationModules(InjectionModel model) { //加载待注入模块 var pendinglist = model.Modules.Select(module => module).ToList(); //加载需要注入模块 var is_load = false; while (pendinglist.Count > 0) { foreach (var module in pendinglist.Select(module => module).ToList()) { //判断当前是否可以注入 if (module.ConstructorList.All( constructor => string.IsNullOrEmpty(constructor.Ref) || model.InstanceCollection.ContainsKey(constructor.Ref))) { model.InstanceCollection.Add( module.Name, InstantiationModule(module, module.ConstructorList.ToDictionary( constructor => constructor.Name, constructor => string.IsNullOrEmpty(constructor.Ref) ? constructor.Value : model.InstanceCollection[constructor.Ref]))); pendinglist.Remove(module); is_load = true; } } if (is_load) { is_load = false; } else { throw new System.Exception("存在无法实例化的模块, 请检查是否配置正确", new System.Exception(pendinglist.ToType <string>())); } } //加载属性 foreach (var module in model.Modules) { var instance = model.InstanceCollection[module.Name]; foreach (var property in module.PropertyList) { instance.GetType().GetProperty(property.Name).SetValue( model.InstanceCollection[module.Name], string.IsNullOrEmpty(property.Ref) ? property.Value : model.InstanceCollection[property.Ref]); } } return(model); }