/// <summary> /// 设置配置类的映射关系 /// </summary> /// <param name="configClassType">配置类的类型</param> /// <param name="configPropertyInfo">配置属性的信息</param> /// <param name="disconfAttribute">配置特性</param> private static void SetConfigClassMapper(Type configClassType, PropertyInfo configPropertyInfo, DisconfAttribute disconfAttribute) { if (disconfAttribute == null) { throw new ArgumentNullException("disconfAttribute"); } if (string.IsNullOrWhiteSpace(disconfAttribute.Name)) { throw new ArgumentNullException("disconfAttribute.Name"); } if (!ConfigStorage.ContainsKey(disconfAttribute.Name)) { throw new Exception(string.Format("配置中心未配置名为:{0}的配置项!", disconfAttribute.Name)); } //将一些常用的配置文件的扩展名匹配到指定的数据转换器 Type dataConverterType = disconfAttribute.DataConverterType; if (dataConverterType == null) { if (disconfAttribute.Name.EndsWith(".json")) { dataConverterType = typeof(JsonDataConverter); } else if (disconfAttribute.Name.EndsWith(".config")) { dataConverterType = typeof(AppSettingsDataConverter); } else if (disconfAttribute.Name.EndsWith(".properties")) { dataConverterType = typeof(PropertiesDataConverter); } else { dataConverterType = typeof(DefalutDataConverter); } } //根据配置类的类型或配置属性的类型注册对应的数据转换器 IDataConverter dataConverter = (IDataConverter)Activator.CreateInstance(dataConverterType, true); Type registerType = configPropertyInfo == null ? configClassType : configPropertyInfo.PropertyType; DataConverterManager.RegisterDataConverter(registerType, dataConverter); ConfigStorageItem item = ConfigStorage[disconfAttribute.Name]; ConfigClassMapper mapper = new ConfigClassMapper { //DisconfNodeType = disconfAttribute.DisconfNodeType, ConfigPropertyInfo = configPropertyInfo, ConfigClassType = configClassType }; item.ConfigClassMapper = mapper; item.RefreshConfigObject(); if (disconfAttribute.CallbackType != null) { ICallback callback = (ICallback)Activator.CreateInstance(disconfAttribute.CallbackType, true); ConfigCallbackManager.RegisterCallback(disconfAttribute.Name, callback); item.ConfigClassMapper.ConfigNodeName = disconfAttribute.Name; } ConfigStorage[item.Name] = item; }
/// <summary> /// 扫描配置类 /// </summary> /// <param name="assemblies"></param> private static void AssemblyScan(params Assembly[] assemblies) { if (!string.IsNullOrWhiteSpace(DisconfClientSettings.ConfigAssemblies)) { List <Assembly> list = new List <Assembly>(assemblies); string[] array = DisconfClientSettings.ConfigAssemblies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (array.Length > 0) { foreach (string assemby in array) { list.Add(Assembly.Load(assemby)); } } assemblies = list.ToArray(); } if (assemblies == null || assemblies.Length <= 0) { assemblies = GetAllAssemblies().ToArray(); } foreach (var assembly in assemblies) { if (assembly == null) { continue; } try { Type[] types = assembly.GetTypes(); foreach (var type in types) { bool flag = false; DisconfAttribute classAttribute = Utils.GetFirstAttribute <DisconfAttribute>(type, true); if (classAttribute != null) { SetConfigClassMapper(type, null, classAttribute); flag = true; } PropertyInfo[] propertyInfos = type.GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { DisconfAttribute propertyAttribute = Utils.GetFirstAttribute <DisconfAttribute>(propertyInfo, true); if (propertyAttribute != null) { SetConfigClassMapper(type, propertyInfo, propertyAttribute); flag = true; } } if (flag) { LogManager.GetLogger().Info(string.Format("Regsiter ConfigClass:{0}", type.GetFullTypeName())); } } } catch (Exception ex) { LogManager.GetLogger().Warn(string.Format("AssemblyScan:{0}", assembly.FullName)); } } }