加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

配置文件_自定义section标签获取数据

发布时间:2020-12-16 08:57:00 所属栏目:asp.Net 来源:网络整理
导读:前言:为了节约时间,先只粘贴关键代码: 1-添加section标签,name为自定义标签名称,type为:命名空间+类型,程序集名称 section name ="watchModel" type ="DataCommon.Help.WatchModel,DataCommon" / 2-自定义标签数据: watchModel为自定义标签(Configu

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

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" />
      下   班="17:50:00"="18:05:00"="xiaban" 每日BUG="3"="09:10:00"="0"="MyProjectBugTips"="" 吃饭提醒="4"="11:35:00"="11:40:00"="chifan" 项目上线临时时间="5"="14:05:00"="17:15:00"="10"="30"="bugCheck" />
    </>
  >

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>
        static T GetSectionT<T>(string sectionName) where T : 
        {
            T t = ConfigurationManager.GetSection(sectionName) as T;
            return t;
        }
}

?

WatchModel watchModel = ConfigHelper.GetSectionT<WatchModel>("watchModel");

?

namespace DataCommon.Help
{
     WatchModel : ConfigurationSection
    {
        [ConfigurationProperty(watchItems")]
        public WatchItems WatchItems
        {
            get
            {
                return this["]  WatchItems;
            }
            set"] = value;
            }
        }
    }

     WatchItems : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            new WatchItem();
        }

        override object GetElementKey(ConfigurationElement element)
        {
             ((WatchItem)element).ID;
        }

        public WatchItem this[ id]
        {
            return (WatchItem)base.BaseGet(id);
            }
        }
    }

     WatchItem : ConfigurationElement
    {
         唯一标识
        </summary>
        [ConfigurationProperty(IDint ID
        {
            return (int)];
            }
             value;
            }
        }
         是否启用
        IsEnablebool IsEnable
        {
            bool) 开始时间(误差1秒=取决于计时器默认时间间隔)
        BeginTimestring BeginTime
        {
            string) 结束时间
        EndTime EndTime
        {
             最大执行次数
        MaxActionTimes MaxActionTimes
        {
             计时周期内执行的动作(动作会在到达开始时间后的)
        ActionName ActionName
        {
             计时周期内执行的动作传入数据(动作会在到达开始时间后的)
        ActionData ActionData
        {
             动作执行时间间隔(秒)
        ActionSeconds ActionSeconds
        {
             value;
            }
        }
    }
}

?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读