C# web.config文件配置属性映射到实体代码
public class TFSApiOptionsHelper
{
private static TFSApiOptionsHelper _onfigHelper = null;
private static readonly object locker = new object();
private static object lockHelper = new object();
private static TFSApiOptions _options;
static XmlDocument doc = new XmlDocument();
//取配置地址
public static string ConfigBasePath = AppDomain.CurrentDomain.BaseDirectory;
public static TFSApiOptionsHelper GetWebConfig
{
get
{
if (_onfigHelper == null)
{
lock (locker)
{
string[] configFileName = new string[] { ConfigBasePath, "web.config" };
string filePath = Path.Combine(configFileName);
_onfigHelper = new TFSApiOptionsHelper();
doc.Load(filePath);
}
}
return _onfigHelper;
}
}
/// <summary>
/// 取配置文件
/// </summary>
/// <returns></returns>
public static TFSApiOptions GetConfig()
{
if(_options !=null)
{
return _options;
}
string[] configFileName = new string[] { ConfigBasePath, "config", "tfs.config" };
string filePath = Path.Combine(configFileName);
//如果配置 文件存在
if (System.IO.File.Exists(filePath))
{
_options = Deserialize(filePath);
}
return _options;
}
/// <summary>
///
/// </summary>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static TFSApiOptions Deserialize(string configFilePath)
{
lock (lockHelper)
{
return (TFSApiOptions)SerializationHelper.Load(typeof(TFSApiOptions), configFilePath);
}
}
/// <summary>
/// 查找 是否有 配置 WeChatPayNotify 节点
/// </summary>
public bool IsTFSApiPayNotify
{
get
{
bool flag = false;
XmlNode node = doc.SelectSingleNode("configuration/system.webServer/ handlers");
//doc.SelectSingleNode("configuration/system.webServer/ handlers").ChildNodes[0].Attributes["name"]
if (node == null || node.ChildNodes.Count == 0)
{
return flag;
}
foreach (XmlNode item in node.ChildNodes)
{
if (item.Attributes != null && item.Attributes["name"] != null)
{
string name = item.Attributes["name"].Value;
//找到 配置节点 名称 返回
if (name == "PayNotify")
{
flag = true;
return flag;
}
}
}
return flag;
}
}
}
/// <summary>
/// SerializationHelper 的摘要说明。
/// </summary>
public class SerializationHelper
{
private SerializationHelper()
{ }
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
public static XmlSerializer GetSerializer(Type t)
{
int type_hash = t.GetHashCode();
if (!serializer_dict.ContainsKey(type_hash))
serializer_dict.Add(type_hash, new XmlSerializer(t));
return serializer_dict[type_hash];
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static object Load(Type type, string filename)
{
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static bool Save(object obj, string filename)
{
bool success = false;
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
success = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
return success;
}
/// <summary>
/// xml序列化成字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>xml字符串</returns>
public static string Serialize(object obj)
{
string returnStr = "";
XmlSerializer serializer = GetSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = null;
StreamReader sr = null;
try
{
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = System.Xml.Formatting.Indented;
serializer.Serialize(xtw, obj);
ms.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(ms);
returnStr = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (xtw != null)
xtw.Close();
if (sr != null)
sr.Close();
ms.Close();
}
return returnStr;
}
public static object DeSerialize(Type type, string s)
{
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
try
{
XmlSerializer serializer = GetSerializer(type);
return serializer.Deserialize(new MemoryStream(b));
}
catch (Exception ex)
{
throw ex;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构