前言:为了节约时间,先只粘贴关键代码:

1-添加section标签,name为自定义标签名称,type为:命名空间+类型,程序集名称

<section name="watchModel" type="DataCommon.Help.WatchModel,DataCommon" />

2-自定义标签数据:

  watchModel为自定义标签(ConfigurationSection),watchItems为自定义标签的数据集(ConfigurationElementCollection);add为数据集里的model(ConfigurationElement)。

<watchModel>
<watchItems>
<!--上 班-->
<add ID="1" IsEnable="true" BeginTime="09:05:00" EndTime="09:15:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="shangban" />
<!--下 班-->
<add ID="2" IsEnable="true" BeginTime="17:50:00" EndTime="18:05:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="xiaban" />
<!--每日BUG-->
<add ID="3" IsEnable="true" BeginTime="09:10:00" EndTime="09:15:00" MaxActionTimes="1" ActionSeconds="0" ActionName="MyProjectBugTips" ActionData="" />
<!--吃饭提醒-->
<add ID="4" IsEnable="true" BeginTime="11:35:00" EndTime="11:40:00" MaxActionTimes="2" ActionSeconds="120" ActionName="SendTipsToDingding" ActionData="chifan" />
<!--项目上线临时时间-->
<add ID="5" IsEnable="true" BeginTime="14:05:00" EndTime="17:15:00" MaxActionTimes="10" ActionSeconds="30" ActionName="MyProjectBugTips" ActionData="bugCheck" />
</watchItems>
</watchModel>

3-创建自定义标签Model:

标签分为3部分,代码也对应3个继承类:ConfigurationSection,ConfigurationElementCollection,ConfigurationElement。

类的属性和标签属性使用:ConfigurationProperty("标签属性")进行对应,需要对get,set方法进行改造。

集合标签:需要对key,createElement,和下标获取对象方法,进行重构。

常见错误-1-对象watchModel需要继承ConfigrationSection,总之每个子标签对应的model都需要继承对应的属性,并对其进行改写或重写:

创建 watchModel 的配置节处理程序时出错: 类型“DataCommon.Help.WatchModel”不从“System.Configuration.IConfigurationSectionHandler”继承。

public class ConfigHelper{
/// <summary>
/// 获取Section对象数据集
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetSectionT<T>(string sectionName) where T : class
{
T t = ConfigurationManager.GetSection(sectionName) as T;
return t;
}
}
WatchModel watchModel = ConfigHelper.GetSectionT<WatchModel>("watchModel");
namespace DataCommon.Help
{
public class WatchModel : ConfigurationSection
{
[ConfigurationProperty("watchItems")]
public WatchItems WatchItems
{
get
{
return this["watchItems"] as WatchItems;
}
set
{
this["watchItems"] = value;
}
}
} public class WatchItems : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new WatchItem();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((WatchItem)element).ID;
} public WatchItem this[object id]
{
get
{
return (WatchItem)base.BaseGet(id);
}
}
} public class WatchItem : ConfigurationElement
{
/// <summary>
/// 唯一标识
/// </summary>
[ConfigurationProperty("ID")]
public int ID
{
get
{
return (int)this["ID"];
}
set
{
this["ID"] = value;
}
}
/// <summary>
/// 是否启用
/// </summary>
[ConfigurationProperty("IsEnable")]
public bool IsEnable
{
get
{
return (bool)this["IsEnable"];
}
set
{
this["IsEnable"] = value;
}
}
/// <summary>
/// 开始时间(误差1秒=取决于计时器默认时间间隔)
/// </summary>
[ConfigurationProperty("BeginTime")]
public string BeginTime
{
get
{
return (string)this["BeginTime"];
}
set
{
this["BeginTime"] = value;
}
}
/// <summary>
/// 结束时间
/// </summary>
[ConfigurationProperty("EndTime")]
public string EndTime
{
get
{
return (string)this["EndTime"];
}
set
{
this["EndTime"] = value;
}
}
/// <summary>
/// 最大执行次数
/// </summary>
[ConfigurationProperty("MaxActionTimes")]
public int MaxActionTimes
{
get
{
return (int)this["MaxActionTimes"];
}
set
{
this["MaxActionTimes"] = value;
}
}
/// <summary>
/// 计时周期内执行的动作(动作会在到达开始时间后的)
/// </summary>
[ConfigurationProperty("ActionName")]
public string ActionName
{
get
{
return (string)this["ActionName"];
}
set
{
this["ActionName"] = value;
}
}
/// <summary>
/// 计时周期内执行的动作传入数据(动作会在到达开始时间后的)
/// </summary>
[ConfigurationProperty("ActionData")]
public string ActionData
{
get
{
return (string)this["ActionData"];
}
set
{
this["ActionData"] = value;
}
}
/// <summary>
/// 动作执行时间间隔(秒)
/// </summary>
[ConfigurationProperty("ActionSeconds")]
public int ActionSeconds
{
get
{
return (int)this["ActionSeconds"];
}
set
{
this["ActionSeconds"] = value;
}
}
}
}

总结:以上就是主要的代码了,中间也遇到过一些问题,上面基本上都写了,以后再补充优化吧。

配置文件_自定义section标签获取数据的更多相关文章

  1. Flutter实战视频-移动电商-08.Dio基础_伪造请求头获取数据

    08.Dio基础_伪造请求头获取数据 上节课代码清楚 重新编写HomePage这个动态组件 开始写请求的方法 请求数据 .但是由于我们没加请求的头 所以没有返回数据 451就是表示请求错错误 创建请求 ...

  2. 08-Flutter移动电商实战-dio基础_伪造请求头获取数据

    在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.这篇文章就以极客时间 为例,讲一下通过伪造 ...

  3. Flutter移动电商实战 --(8)dio基础_伪造请求头获取数据

    在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.这篇文章就以极客时间 为例,讲一下通过伪造 ...

  4. Struts2【UI标签、数据回显、资源国际化】

    Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签...也就是显示页面的标签..... 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再 ...

  5. Flutter实战视频-移动电商-09.首页_项目结构建立和获取数据

    09.首页_项目结构建立和获取数据 在config下创建service_url.dart 用来配置我们后端接口的配置文件 一个变量存 接口地址,一个接口方法地址 所有后天请求数据的方法都放在这个文件夹 ...

  6. 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”

    摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...

  7. Android 开发 values目录里定义数组、颜色、文本、尺寸xml配置文件并且获取数据 附录Android符号转码表

    以下xml都在res/values/文件夹下创建 创建String类型array: /app/src/main/res/values/array.xml <?xml version=" ...

  8. struts2使用jsp和<s:property>标签获取json格式的返回数据

    struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...

  9. Springboot中使用自定义参数注解获取 token 中用户数据

    使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...

随机推荐

  1. js对文中某一处关键字自动检索和全文检索

    部分检索: 代码: <%@ page language="java" contentType="text/html; charset=utf-8" pag ...

  2. 集合系列 Queue(九):PriorityQueue

    PriorityQueue 是一个优先级队列,其底层原理采用二叉堆实现.我们先来看看它的类声明: public class PriorityQueue<E> extends Abstrac ...

  3. 围观高手是如何写好 Python 循环,把内存用到极致的?

    0 前言 说到处理循环,我们习惯使用for, while等,比如依次打印每个列表中的字符: lis = ['I', 'love', 'python'] for i in lis:     print( ...

  4. Python Weekly 419

    文章,教程或讲座 如何用 Dropbox Security 构建用于日志系统的威胁检测和事件响应的工具 https://blogs.dropbox.com/tech/2019/10/how-dropb ...

  5. Windows10安装ubuntu16.04双系统教程

    写在前面:本教程为windows10安装ubuntu16.04(64位)双系统教程,是我多次安装双系统的经验总结,安装方法同样适用于ubuntu18.04(64位).为了直观和易于理解,我会尽量图文并 ...

  6. 一、I/O模型之BIO

    I/O模型之BIO 基本介绍 Java BIO 就是传统的 Java IO 编程,其相关的类和接口再 java.io 包下 BIO(blocking I/O):同步阻塞,服务器实现模式为一个连接一个线 ...

  7. (转)python中用logging实现日志滚动和过期日志删除

    转自:https://blog.csdn.net/ashi198866/article/details/46725813 logging库提供了两个可以用于日志滚动的class(可以参考https:/ ...

  8. python 对字典分别按照key值、value值进行排序

    1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...

  9. SSH框架之Hibernate第一篇

    1.2Hibernate的概述: 1.2.1 什么Hibernate? Hibernate(开发源代码的对象关系映射框架)是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它 ...

  10. 上传图片到七牛云(服务端 node.js sdk)

    大体思路 前端要上传图片到七牛云,需要有一个token进行授权操作,而获取这个上传的upload token(以下简称upToken),在服务端需要一定的身份校验,比如说:只有登录的vip用户才能拿到 ...