using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace iWareTestForm
|
{
|
public sealed class InterfaceSection : ConfigurationSection
|
{
|
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(TheKeyValueCollection), null,
|
ConfigurationPropertyOptions.IsDefaultCollection);
|
|
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
|
public TheKeyValueCollection KeyValues
|
{
|
get
|
{
|
return (TheKeyValueCollection)base[s_property];
|
}
|
}
|
|
public static InterfaceSection GetConfig()
|
{
|
InterfaceSection configSection = (InterfaceSection)ConfigurationManager.GetSection("InterfaceSection");
|
if (configSection == null)
|
throw new ConfigurationErrorsException("Section [InterfaceSection] is not found.");
|
return configSection;
|
}
|
|
public static InterfaceSection GetConfig(string configPath)
|
{
|
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configPath };
|
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
|
InterfaceSection configSection = (InterfaceSection)config.GetSection("InterfaceSection");
|
if (configSection == null)
|
throw new ConfigurationErrorsException("Section [InterfaceSection] is not found.");
|
return configSection;
|
}
|
}
|
[ConfigurationCollection(typeof(TheKeyValue))]
|
public class TheKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
|
{
|
new TheKeyValue this[string name]
|
{
|
get
|
{
|
return (TheKeyValue)base.BaseGet(name);
|
}
|
}
|
|
// 下面二个方法中抽象类中必须要实现的。
|
protected override ConfigurationElement CreateNewElement()
|
{
|
return new TheKeyValue();
|
}
|
|
protected override object GetElementKey(ConfigurationElement element)
|
{
|
return ((TheKeyValue)element).Name;
|
}
|
}
|
|
public class TheKeyValue : ConfigurationElement
|
{
|
[ConfigurationProperty("name", IsRequired = true)]
|
public string Name
|
{
|
get { return this["name"].ToString(); }
|
set { this["name"] = value; }
|
}
|
|
[ConfigurationProperty("url", IsRequired = true)]
|
public string Url
|
{
|
get { return this["url"].ToString(); }
|
set { this["url"] = value; }
|
}
|
}
|
}
|