配置文件_自定义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工具类 ...
 
随机推荐
- [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
			
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
 - lombok深入实践
			
官网视频 官网地址:https://projectlombok.org 官网的首页视频演示在eclipse中如何使用Lombok; Project Lombok is a java library t ...
 - WPF TreeView 虚拟化-设置滚动到选中项
			
前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数 ...
 - Git - Git分支管理策略
			
前言 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的c ...
 - JavaScript-三种弹窗方式
			
0918自我总结 JavaScript-三种弹窗方式 一.alert 带内容的弹框 用法: <script> alert('弹窗显示的内容') //会弹出框没有点确定不会执行下面的代码会发 ...
 - Java入门——在Linux环境下安装JDK并配置环境变量
			
Java入门——在Linux环境下安装JDK并配置环境变量 摘要:本文主要说明在Linux环境下JDK的安装,以及安装完成之后环境变量的配置. 使用已下载的压缩包进行安装 下载并解压 在Java的官网 ...
 - cesium 入门开发系列矢量瓦片加载展示(附源码下载)
			
前言 cesium 入门开发系列环境知识点了解:cesium api文档介绍,详细介绍 cesium 每个类的函数以及属性等等cesium 在线例子 内容概览 cesium 实现矢量瓦片加载效果 源代 ...
 - expect 知识与示例说明
			
expect 知识与示例说明 2012/04/10 chenxin 2019/07/07 update Chenxin 参考 https://www.cnblogs.com/yinghao1991/p ...
 - Scrapy的下载中间件
			
下载中间件 简介 下载器,无法执行js代码,本身不支持代理 下载中间件用来hooks进Scrapy的request/response处理过程的框架,一个轻量级的底层系统,用来全局修改scrapy的re ...
 - [Linux] Nginx服务下统计网站的QPS
			
单位时间的请求数就是QPS,那么在nginx服务的网站下,如果要统计QPS并且按从高到低排列,需要使用awk配合sort进行处理awk做的主要工作是把access每行日志按分隔符分开,然后循环每一行, ...