schangxiang@126.com
2025-03-10 8706d6948d9a922d8e5adce27f49790ad4ac1d71
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ServiceClient
{
    public sealed class ServiceSection : 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 ServiceSection GetConfig()
        {
            ServiceSection configSection = (ServiceSection)ConfigurationManager.GetSection("ServiceSection");
            if (configSection == null)
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            return configSection;
        }
 
        public static ServiceSection GetConfig(string configPath)
        {
            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configPath };
            var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            ServiceSection configSection = (ServiceSection)config.GetSection("ServiceSection");
            if (configSection == null)
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            return configSection;
        }
        /// <summary>
        /// 获取指定的配置对象
        /// </summary>
        /// <param name="keyName">配置名称</param>
        /// <returns></returns>
        public static TheKeyValue GetTheKeyValueCollection(string keyName)
        {
            ServiceSection configSection = (ServiceSection)ConfigurationManager.GetSection("ServiceSection");
            if (configSection == null)
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            foreach (TheKeyValue item in configSection.KeyValues)
            {
                if (item.Name == keyName)
                    return item;
            }
            return null;
        }
    }
   
}