222
schangxiang@126.com
2024-11-28 88079606346044bb3315a8165d8d6219c72240e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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; }
        }
    }
}