配置文件_自定义section标签获取数据
前言:为了节约时间,先只粘贴关键代码:
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标签获取数据的更多相关文章
- Flutter实战视频-移动电商-08.Dio基础_伪造请求头获取数据
08.Dio基础_伪造请求头获取数据 上节课代码清楚 重新编写HomePage这个动态组件 开始写请求的方法 请求数据 .但是由于我们没加请求的头 所以没有返回数据 451就是表示请求错错误 创建请求 ...
- 08-Flutter移动电商实战-dio基础_伪造请求头获取数据
在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.这篇文章就以极客时间 为例,讲一下通过伪造 ...
- Flutter移动电商实战 --(8)dio基础_伪造请求头获取数据
在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.这篇文章就以极客时间 为例,讲一下通过伪造 ...
- Struts2【UI标签、数据回显、资源国际化】
Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签...也就是显示页面的标签..... 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再 ...
- Flutter实战视频-移动电商-09.首页_项目结构建立和获取数据
09.首页_项目结构建立和获取数据 在config下创建service_url.dart 用来配置我们后端接口的配置文件 一个变量存 接口地址,一个接口方法地址 所有后天请求数据的方法都放在这个文件夹 ...
- 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”
摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...
- Android 开发 values目录里定义数组、颜色、文本、尺寸xml配置文件并且获取数据 附录Android符号转码表
以下xml都在res/values/文件夹下创建 创建String类型array: /app/src/main/res/values/array.xml <?xml version=" ...
- struts2使用jsp和<s:property>标签获取json格式的返回数据
struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...
- Springboot中使用自定义参数注解获取 token 中用户数据
使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...
随机推荐
- IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值1
CSS中的样式属性比较多,经常使用的属性可以分为这么几类:字体.文本.背景.位置.边框.列表,以及其他一些样式属性.每个类中的属性都可以单独使用:如果同一个类中多个属性一起使用,还可以将它们整合为一行 ...
- 【xAsset框架】HFS 轻量级HTTP Server快速入门指南
一.引子 最近马三有幸参与开发了一个简易轻量的Unity资源管理框架 xAsset ,xasset 提供了一种使用资源路径的简单的方式来加载资源,简化了Unity项目资源打包,更新,加载,和回收的作业 ...
- Glide缓存流程
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/cPLkefpEb3w12-uoiqzTig作者:连凌能 Android上图片加载的解决方案有多 ...
- 备用APC队列
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 备用APC队列 占坑:这一节跟进程挂靠相关联,我们先把进程挂靠给 ...
- python django-admin.py startproject xxx 错误:from django.core import management
1. Python安装路径以及Python安装路径\Script文件夹,已经添加到PATH环境变量中. 2. 查看django 版本正常: import django print(django.__v ...
- 剑指offer笔记面试题11----旋转数组的最小数字
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如,数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...
- 总结在ssm整合中,Mybatis出现Mapped Statements collection already contains value for xxxxx的解决方案
先贴一段报错信息: 前面的都不是很重要,看最后灰色标注的那段.... 严重: 异常将上下文初始化事件发送到类的侦听器实例.[org.springframework.web.context.Contex ...
- 微信小程序新服务消息推送 —— 订阅消息
微信团队前不久公测了「订阅消息」,原有的小程序模板消息接口将于 2020 年 1 月 10 日下线,届时将无法发送模板消息.「订阅消息」将完全替代「模板消息」,这两天得空测试了一波. 1.下发权限机制 ...
- 记录:c#实现微信,支付宝扫码支付(一)
因为公司系统业务需要,这几天了解了一下微信和支付宝扫码支付的接口,并用c#实现了微信和支付宝扫码支付的功能. 微信支付分为6种支付模式:1.付款码支付,2.native支付,3.jsapi支付,4.a ...
- nmap的简单使用
主机探测 1.扫描单个主机 nmap 192.168.1.2 2.扫描整个子网 nmap 192.168.1.1/24 3.扫描多个目标 nmap 192.168.1.1 192.168.1.1.5 ...