前言:为了节约时间,先只粘贴关键代码:
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,和下标获取对象方法,进行重构。
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; } } } }
总结:以上就是主要的代码了,中间也遇到过一些问题,上面基本上都写了,以后再补充优化吧。
原文地址:https://www.cnblogs.com/lxhbky/p/11047112.html
时间: 2024-10-10 11:11:06