67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
|
|
namespace WX.CRM.DAL.Redis
|
|
{
|
|
public class RedisConfigurationSection : ConfigurationSection
|
|
{
|
|
//private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(RedisSettingsElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
|
|
//[ConfigurationCollection(typeof(RedisSettingsElementCollection), AddItemName = "RedisSettings")]
|
|
[ConfigurationProperty("", IsDefaultCollection = true)]
|
|
public RedisSettingsElementCollection RedisSettings
|
|
{
|
|
get { return (RedisSettingsElementCollection)base[""]; }
|
|
}
|
|
public static RedisConfigurationSection GetSection()
|
|
{
|
|
return ConfigurationManager.GetSection("redis") as RedisConfigurationSection;
|
|
}
|
|
}
|
|
|
|
[ConfigurationCollection(typeof(RedisSettingsElement))]
|
|
public class RedisSettingsElementCollection : ConfigurationElementCollection, IEnumerable<RedisSettingsElement>
|
|
{
|
|
public RedisSettingsElementCollection()
|
|
: base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
|
|
{
|
|
}
|
|
protected override ConfigurationElement CreateNewElement()
|
|
{
|
|
return new RedisSettingsElement();
|
|
}
|
|
|
|
protected override object GetElementKey(ConfigurationElement element)
|
|
{
|
|
return ((RedisSettingsElement)element).Name;
|
|
}
|
|
|
|
// 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
|
|
public RedisSettingsElement this[string name]
|
|
{
|
|
get { return (RedisSettingsElement)base.BaseGet(name); }
|
|
}
|
|
|
|
public new IEnumerator<RedisSettingsElement> GetEnumerator()
|
|
{
|
|
var e = base.GetEnumerator();
|
|
while (e.MoveNext())
|
|
{
|
|
yield return (RedisSettingsElement)e.Current;
|
|
}
|
|
}
|
|
}
|
|
public class RedisSettingsElement : ConfigurationElement
|
|
{
|
|
[ConfigurationProperty("name")]
|
|
public string Name { get { return (string)base["name"]; } }
|
|
|
|
[ConfigurationProperty("connectionString", IsRequired = true)]
|
|
public string ConnectionString { get { return (string)base["connectionString"]; } }
|
|
|
|
[ConfigurationProperty("db", DefaultValue = 0)]
|
|
public int Db { get { return (int)base["db"]; } }
|
|
|
|
}
|
|
}
|