随着项目的扩展,单独的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文件 实体转换的更多相关文章

  1. c# 根据配置文件路径,设置和获取config文件 appSettings 节点值

    /// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...

  2. 对获取config文件的appSettings节点简单封装

    转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...

  3. C# 获取config文件的值

    自定义配置文件帮助类 利用ExeConfigurationFileMap类将自定义配置文件转换为Configuration类进行数据读取 代码很简单,就不做扼要说明 /// <summary&g ...

  4. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  5. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  6. C# 获取或设置指定 config 文件的值

    ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...

  7. 在.net中读写config文件的各种方法

    阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...

  8. IOC注入框架——Unity中Web.Config文件的配置与调用

    Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...

  9. Unity中Web.Config文件的配置与调用

    在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3 ...

随机推荐

  1. jquery chosen 插件多选初始化

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. leetcode 缺失数字

    给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...

  3. Kettle有什么功能

    转载地址:https://www.cnblogs.com/gala1021/p/7814712.html 简介 Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux. ...

  4. 网易云首席安全架构师谈安全新形势:DDOS两三天,游戏玩家数从几万降到几百

    本文由  网易云发布. 安全是一个永恒的话题,在业务不断云化.攻击越来越复杂的当下,互联网安全呈现了出什么样的严峻形势?对这些形势,网易云又是如何应对的? 网易云首席安全架构师沈明星 4月13日,网易 ...

  5. linux ab压力测试工具及ab命令详解

    原文链接:https://blog.csdn.net/qq_27517377/article/details/78794409 yum -y install httpd-tools ab -v 查看a ...

  6. Redis的快照

    博客链接:http://www.cnblogs.com/zhenghongxin/p/8669913.html redis 本地持久化到硬盘有两种方式,一是快照(snapshotting),二是只追加 ...

  7. 638. Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  8. BZOJ 1305--[CQOI2009]dance跳舞(最大流)

    1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4150  Solved: 1792[Submit][St ...

  9. spring boot 使用application.properties 进行外部配置

    application.properties大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成war包或者jar用于生产环境时,我们可以手动修改环 ...

  10. 如何在for循环中使用多线程

    import java.util.concurrent.Executor;import java.util.concurrent.Executors; public class Test {priva ...