C# 获取config文件 实体转换
随着项目的扩展,单独的key,value配置文件已经不能满足需求了
这里需要自定义配置节点,例如
<!--自定义 具体实体类配置问节点信息-->
<School Name="红旗小学" Number="" Address="北京市,西城区……"></School>
当然,这样的节点可以有多重获取方式,讲自己认为比较好的方式总结一下,仅供参考
可以将当前节点当做试题Model来配置,这样方便管理和操作,下面说明下如何进行转换
1、首先定义实体,因为需要转换,这里用到了configSections的section 节点,配置实体的命名空间
然后试题还需要继承ConfigurationSection类
namespace Model
{
/// <summary>
/// 学校实体
/// </summary>
public class School : ConfigurationSection
{
//获取属性名称
[ConfigurationProperty("Name", IsRequired = false)]
public string Name
{
get
{
//返回配置文件属性值
return this["Name"].ToString();
}
}
[ConfigurationProperty("Number", IsRequired = false)]
public int Number
{
get
{
int i = ;
int.TryParse(this["Number"].ToString(), out i);
return i;
}
}
[ConfigurationProperty("Address", IsRequired = false)]
public string Address
{
get
{
return this["Address"].ToString();
}
}
}
}
2、理由ExeConfigurationFileMap类将自定义配置文件转换为Configuration类
然后从Configuration类中获取section节点属性,转换为Model实体进行返回
#region 初始化配置
/// <summary>
/// 初始化配置文件
/// </summary>
private static void ConfigDataLoad()
{
//获取文件路径
string fileName = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\TestModel.config";
if (File.Exists(fileName))
{
ExeConfigurationFileMap file = new ExeConfigurationFileMap
{
ExeConfigFilename = fileName
};
//将文件转换为Configuration
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
//初始化对象
SetCustomModel(config);
}
else
{
throw new Exception("配置文件不存在");
}
}
/// <summary>
/// 初始化值配置文件为实体Model
/// </summary>
private static void SetCustomModel(Configuration config)
{
_school = ((School)config.GetSection("School"));
}
#endregion
完整帮助类代码如下
/// <summary>
/// 自定义配置文件 实体转换
/// </summary>
public class ConfigToModelHelper
{
#region 初始化自定义节点为Model
private static School _school;
public static School School
{
get
{
if (_school == null)
{
ConfigDataLoad();
}
return _school;
}
}
#endregion #region 初始化配置 /// <summary>
/// 初始化配置文件
/// </summary>
private static void ConfigDataLoad()
{
//获取文件路径
string fileName = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\TestModel.config";
if (File.Exists(fileName))
{
ExeConfigurationFileMap file = new ExeConfigurationFileMap
{
ExeConfigFilename = fileName
};
//将文件转换为Configuration
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); //初始化对象
SetCustomModel(config);
}
else
{
throw new Exception("配置文件不存在");
}
}
/// <summary>
/// 初始化值配置文件为实体Model
/// </summary>
private static void SetCustomModel(Configuration config)
{
_school = ((School)config.GetSection("School"));
}
#endregion }
调用:
School school = ConfigToModelHelper.School;
C# 获取config文件 实体转换的更多相关文章
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- 对获取config文件的appSettings节点简单封装
转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...
- C# 获取config文件的值
自定义配置文件帮助类 利用ExeConfigurationFileMap类将自定义配置文件转换为Configuration类进行数据读取 代码很简单,就不做扼要说明 /// <summary&g ...
- 获取 config文件的节点值
System.Configuration.ConfigurationManager.AppSettings["followTemplate"];
- 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...
- C# 获取或设置指定 config 文件的值
ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...
- 在.net中读写config文件的各种方法
阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...
- IOC注入框架——Unity中Web.Config文件的配置与调用
Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...
- Unity中Web.Config文件的配置与调用
在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3 ...
随机推荐
- 在每页(分页)报表中重复显示标题 - SQL Server Reporting Service (SSRS)
问题描述 TFS系统提供多种报表,有图表(Chart).Web面板(Dashboard).SharePoint面板.Excel报表,SQL Server Reporting Serivce(SSRS) ...
- C# 中数组、ArrayList、List<T> 区别
一:数组 //定义 ]; //赋值 strs[] = "A"; strs[] = "B"; //修改 strs[] = "C"; //取值 ...
- Linq to SQL 练习
public class HomeController : Controller { // // GET: /Home/ empentity entity = new empentity(); pub ...
- iis 中https修改主机名方法
来源地址: https://www.pianyissl.com/support/page/29
- c# 字符串去掉两端空格,并且将字符串中多个空格替换成一个空格
字符串去掉两端空格,并且将字符串中多个空格替换成一个空格: 主要还是考察使用字符串的方法: trim(); 去掉字符串两端空格 split(); 切割 string.join(); 连接 class ...
- [原创] 基础中的基础(二):C/C++ 中 const 修饰符用法总结
在这篇文章中,我总结了一些C/C++语言中的 const 修饰符的常见用法,供大家参考. const 的用法,也是技术性面试中常见的基础问题,希望能够帮大家梳理一下知识,给大家一点点帮助.作者是菜鸟一 ...
- leetcode 75. 颜色分类 JAVA
题目: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和 ...
- 629. K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 教你利用Node.js漏洞搞事情
PentestingNode.js Application : Nodejs Application Security 原文地址:http://www.websecgeeks.com/2017/04/ ...
- ansj分词史上最详细教程
最近的项目需要使用到分词技术.本着不重复造轮子的原则,使用了ansj_seg来进行分词.本文结合博主使用经过,教大家用最快的速度上手使用ansj分词. 1.给ansj来个硬广 项目的github地址: ...