コード例 #1
0
 /// <summary>
 /// 配置项值变更时的回调处理
 /// </summary>
 public void Callback()
 {
     try
     {
         ICallback callback = null;
         if (this.ConfigClassMapper == null)
         {
             callback = ConfigCallbackManager.GetCallback(this.Name);
         }
         else
         {
             this.RefreshConfigObject();
             callback = ConfigCallbackManager.GetCallback(this.ConfigClassMapper.ConfigNodeName);
         }
         if (callback != null)
         {
             callback.Invoke();
         }
     }
     catch (Exception ex)
     {
         LogManager.GetLogger().Error(string.Format("DisconfClient.ConfigStorageItem.Callback,Name:{0}", this.Name), ex);
     }
 }
コード例 #2
0
        /// <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;
        }