/// <summary> /// 刷新配置类的值 /// </summary> public void RefreshConfigObject() { if (ConfigClassMapper == null) { return; } try { if (ConfigClassMapper.ConfigPropertyInfo == null) { IDataConverter dataConverter = DataConverterManager.GetDataConverter(ConfigClassMapper.ConfigClassType); object value = dataConverter.Parse(ConfigClassMapper.ConfigClassType, Data); ConfigClassMapper.SetConfigClassInstance(ConfigClassMapper.ConfigClassType, value); } else { IDataConverter dataConverter = DataConverterManager.GetDataConverter(ConfigClassMapper.ConfigPropertyInfo.PropertyType); object propertyValue = dataConverter.Parse(ConfigClassMapper.ConfigPropertyInfo.PropertyType, Data); object obj = null; object oldObject = ConfigClassMapper.GetConfigClassInstance(ConfigClassMapper.ConfigClassType); if (oldObject == null) { obj = Activator.CreateInstance(ConfigClassMapper.ConfigClassType, true); } else { obj = oldObject; try { string json = JsonConvert.SerializeObject(oldObject); object newObject = JsonConvert.DeserializeObject(json, ConfigClassMapper.ConfigClassType); obj = newObject; } catch (Exception ex) { LogManager.GetLogger().Error(string.Format("RefreshConfigObject.Clone,Exception:{0}", ex)); } } ConfigClassMapper.ConfigPropertyInfo.SetValue(obj, propertyValue, null); ConfigClassMapper.SetConfigClassInstance(ConfigClassMapper.ConfigClassType, obj); } } catch (Exception ex) { LogManager.GetLogger().Error(string.Format("RefreshConfigObject,Exception:{0}", ex)); } }
public static T GetConfigValue <T>(string key, T defalut = default(T)) { ConfigStorageItem item = ConfigStorageManager.GetConfigStorageItem(key); if (item == null) { return(defalut); } if (typeof(T) == typeof(string)) { return((T)((object)item.Data)); } IDataConverter dataConverter = DataConverterManager.GetDataConverter(key); if (dataConverter == null) { if (key.EndsWith(".json")) { dataConverter = new JsonDataConverter(); } else if (key.EndsWith(".config")) { dataConverter = new AppSettingsDataConverter(); } else if (key.EndsWith(".properties")) { dataConverter = new PropertiesDataConverter(); } else { dataConverter = new DefalutDataConverter(); } } return((T)dataConverter.Parse(typeof(T), item.Data)); }
/// <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; }
static void Main(string[] args) { try { //请将这个初始化的方法调用放在你应用的入口处 ConfigManager.Init(); //在应用的入口处注册定制化的类型转换器,如果是配置类的方式,则可以直接在Disconf特性中设置,例见:PropertiesDemoConfig DataConverterManager.RegisterDataConverter("TestMyList", new MyListDataConverter()); //关于DataConverter //如果你需要当某个节点的值发生变化时获行通知,请在应用的入口处注册配置节点值变更时的回调通知类 ConfigCallbackManager.RegisterCallback("TestMyList", new TestMyListCallback()); //可以将一些配置项归类,定义在一个配置类中 TypeTestConfig typeTestConfig = ConfigManager.GetConfigClass <TypeTestConfig>(); //也可以直接以Key-Value的形式来调用 string testString = ConfigManager.GetConfigValue <string>("TestString"); int testInt = ConfigManager.GetConfigValue <int>("TestInt"); long testLong = ConfigManager.GetConfigValue <long>("TestLong"); uint testUint = ConfigManager.GetConfigValue <uint>("TestUint"); ulong testUlong = ConfigManager.GetConfigValue <ulong>("TestUlong"); float testFloat = ConfigManager.GetConfigValue <float>("TestFloat"); double testDouble = ConfigManager.GetConfigValue <double>("TestDouble"); decimal testDecimal = ConfigManager.GetConfigValue <decimal>("TestDecimal"); DisconfNodeType testEnumInt = ConfigManager.GetConfigValue <DisconfNodeType>("TestEnumInt"); DisconfNodeType testEnumString = ConfigManager.GetConfigValue <DisconfNodeType>("TestEnumString"); Guid testGuid = ConfigManager.GetConfigValue <Guid>("TestGuid"); Type testType = ConfigManager.GetConfigValue <Type>("TestType"); bool testBool = ConfigManager.GetConfigValue <bool>("TestBool"); char testChar = ConfigManager.GetConfigValue <char>("TestChar"); DateTime testDateTime = ConfigManager.GetConfigValue <DateTime>("TestDateTime"); IList <string> testStringList = ConfigManager.GetConfigValue <IList <string> >("TestStringList"); IDictionary <string, string> testStringDictionary = ConfigManager.GetConfigValue <IDictionary <string, string> >("TestStringDictionary"); //可以为某一个配置文件定义一个配置类 AppSettingsDemoConfig appSettingsDemoConfig = ConfigManager.GetConfigClass <AppSettingsDemoConfig>(); //所有的配置项都可以通过这样的方法直接取出配置项中的字符串值 string appSettingsValue = ConfigManager.GetConfigValue <string>("redis.config"); //如果配置项的名称是以.config,.properties扩展名结尾的,或注册过DictionaryDataConverter的则可以转换为Dictionary<string, string>类型 IDictionary <string, string> appSettingsValueToDictionary = ConfigManager.GetConfigValue <IDictionary <string, string> >("redis.config"); //对IDictionary<string, string>类型做了“Get<T>'扩展方法,类型的转换使用的是DefalutDataConverter string host = appSettingsValueToDictionary.Get <string>("Host"); int port = appSettingsValueToDictionary.Get <int>("Port"); //以.json扩展名结尾的,或注册过JsonDataConverter的则可以转换为指定的类型 JsonDemoConfig jsonDemoConfig = ConfigManager.GetConfigClass <JsonDemoConfig>(); string json = ConfigManager.GetConfigValue <string>("redis.json"); JsonDemoConfig jsonDemoConfig2 = ConfigManager.GetConfigValue <JsonDemoConfig>("redis.json"); //以.properties扩展名结尾的,或注册过PropertiesDataConverter的则可以进行类型转换 PropertiesDemoConfig propertiesDemoConfig = ConfigManager.GetConfigClass <PropertiesDemoConfig>(); string properties = ConfigManager.GetConfigValue <string>("redis.properties"); PropertiesDemoConfig propertiesDemoConfig2 = ConfigManager.GetConfigValue <PropertiesDemoConfig>("redis.properties"); //调用本地appSettings中的配置项,类型的转换使用的是DefalutDataConverter string appId = ConfigManager.AppSettings <string>("AppId"); int refreshTime = ConfigManager.AppSettings <int>("DisconfClient.RefreshTime", 15); } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }