chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.IO;
 
namespace PdmAlert
{
    public class ToolSetting
    {
        public static ToolSetting Instance { get; private set; }
 
        private Dictionary<string, string> DataMap;
 
        public int TimeOut { get; private set; } = 600;
        public string Mode { get; private set; } = "prod";
        public string Ip { get; private set; } = "";
        public int Port { get; private set; } = 0;
        public string BaseUrl { get; private set; } = "";
 
        private void Init()
        {
 
        }
 
        static ToolSetting()
        {
            Instance = LoadFromXml(null);
        }
 
        private static ToolSetting LoadFromXml(string xmlPath)
        {
            if (xmlPath == null)
            {
                string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                xmlPath = Path.Combine(dir, "Settings", "ToolSetting.xml");
            }
 
            ToolSetting setting = new ToolSetting();
            if (File.Exists(xmlPath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlPath);
                XmlNode xmlRoot = xmlDoc.DocumentElement;
                BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
                var props = typeof(ToolSetting).GetProperties(flags).ToArray();
                foreach (var prop in props)
                {
                    string name = prop.Name;
                    string value = xmlRoot.SelectSingleNode(name)?.InnerText;
                    if (value == null)
                    {
                        continue;
                    }
                    Type type = prop.PropertyType;
                    if (type == typeof(int))
                    {
                        int.TryParse(value, out int temp);
                        prop.SetValue(setting, temp);
                    }
                    else if (type == typeof(bool))
                    {
                        bool.TryParse(value, out bool temp);
                        prop.SetValue(setting, temp);
                    }
                    else if (type == typeof(string))
                    {
                        prop.SetValue(setting, value);
                    }
                }
            }
            setting.Init();
            return setting;
        }
 
        /// <summary>
        /// 强制刷新配置
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static ToolSetting RefreshFromXml(string xmlPath)
        {
            Instance = LoadFromXml(xmlPath);
            return Instance;
        }
    }
}