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;
|
}
|
}
|
}
|