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

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. nodejs攻略——第一章 配置

    nodejs这玩意吧,我也是心血来潮的学习,所以啊.看这篇文章之前请大家有以下技能,如果没有..我不确定你能看得懂,毕竟我文采水平实在有限. 前置技能要求: c#或java 熟练+ JavaScrip ...

  2. Python 教你识别淘宝刷单,买到称心如意的商品

    发际线堪忧的小 Q,为了守住头发最后的尊严,深入分析了几十款防脱洗发水的评价,最后综合选了一款他认为最完美的防脱洗发水. 一星期后,他没察觉到任何变化. 一个月后,他用卷尺量了量,发际线竟然后退了 0 ...

  3. docker容器中安装vim 、telnet、ifconfig命令

    一.在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found 问题:apt-get install vim安装vim 命令时,提示:如下内 ...

  4. PHP Swoole与TCP三次握手

    握手常见问题 1.连接拒绝 2.Operation now in progress 多是因为丢包.错误ip.backlog满了&阻塞&tcp_abort_on_overflow=0 3 ...

  5. ETCD:HTTP JSON API通过gRPC网关

    原文地址:HTTP JSON API through the gRPC gateway etcd v3 使用 gRPC 作为消息协议.etcd项目包括一个基于gRPC的Go客户端和一个命令行工具,et ...

  6. html+css面试题(一)

    XHTML与HTML的有何异同? HTML是一种基于WEB的网络设计语言,XHTML是基于XML的置标语言,XHTML可以认为是XML版的HTML,所以它的语法比较严谨:元素必须关闭,嵌套必须正确,大 ...

  7. linux下unzip解压报错“symlink error: File name too long”怎么办?提供解决方案。

    点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 分享unzip工具的一个bug." 最近在研究菠菜站,中间用到了Spidermonkey,碰到一些小波折,在这里分享出来,以便大家 ...

  8. hadoop节点动态删除与增加

    动态删除 1)修改配置文件 修改hdfs-site.xml文件,适当减小dfs.replication的数量,增加dfs.hosts.exclude选项 vi hdfs-site.xml <pr ...

  9. ubuntu 18.04下安装JDK

    一.安装前检查 检查是否已经安装 java -version 二.安装方式 1)通过ppa(源) 2)通过官网安装包安装  JDK官网下载地址  或百度云下载地址,提取码 rzq5 三.安装步骤 (一 ...

  10. [Go] go等待读取最后一行的数据内容

    这段代码是参照慕课网的视频教程,主要是f.Seek(0, os.SEEK_END)移动到末尾,但是里面有个小问题,当打开的文件被重新清空内容的清空下,就再也不能到读取数据了,比如在开启读取后 echo ...