深入学习ing

配置文件_自定义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;
            }
        }
    }
}

 

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

posted on 2019-06-18 19:21 深入学习ing 阅读(...) 评论(...) 编辑 收藏

导航