配置文件_自定义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" />
      
      <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{
    /// 
        /// 获取Section对象数据集
        /// 
        /// 
        /// 
        public static T GetSectionT(string sectionName) where T : class
        {
            T t = ConfigurationManager.GetSection(sectionName) as T;
            return t;
        }
}

 

WatchModel watchModel = ConfigHelper.GetSectionT("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
    {
        /// 
        /// 唯一标识
        /// 
        [ConfigurationProperty("ID")]
        public int ID
        {
            get
            {
                return (int)this["ID"];
            }
            set
            {
                this["ID"] = value;
            }
        }
        /// 
        /// 是否启用
        /// 
        [ConfigurationProperty("IsEnable")]
        public bool IsEnable
        {
            get
            {
                return (bool)this["IsEnable"];
            }
            set
            {
                this["IsEnable"] = value;
            }
        }
        /// 
        /// 开始时间(误差1秒=取决于计时器默认时间间隔)
        /// 
        [ConfigurationProperty("BeginTime")]
        public string BeginTime
        {
            get
            {
                return (string)this["BeginTime"];
            }
            set
            {
                this["BeginTime"] = value;
            }
        }
        /// 
        /// 结束时间
        /// 
        [ConfigurationProperty("EndTime")]
        public string EndTime
        {
            get
            {
                return (string)this["EndTime"];
            }
            set
            {
                this["EndTime"] = value;
            }
        }
        /// 
        /// 最大执行次数
        /// 
        [ConfigurationProperty("MaxActionTimes")]
        public int MaxActionTimes
        {
            get
            {
                return (int)this["MaxActionTimes"];
            }
            set
            {
                this["MaxActionTimes"] = value;
            }
        }
        /// 
        /// 计时周期内执行的动作(动作会在到达开始时间后的)
        /// 
        [ConfigurationProperty("ActionName")]
        public string ActionName
        {
            get
            {
                return (string)this["ActionName"];
            }
            set
            {
                this["ActionName"] = value;
            }
        }
        /// 
        /// 计时周期内执行的动作传入数据(动作会在到达开始时间后的)
        /// 
        [ConfigurationProperty("ActionData")]
        public string ActionData
        {
            get
            {
                return (string)this["ActionData"];
            }
            set
            {
                this["ActionData"] = value;
            }
        }
        /// 
        /// 动作执行时间间隔(秒)
        /// 
        [ConfigurationProperty("ActionSeconds")]
        public int ActionSeconds
        {
            get
            {
                return (int)this["ActionSeconds"];
            }
            set
            {
                this["ActionSeconds"] = value;
            }
        }
    }
}

 

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

你可能感兴趣的:(配置文件_自定义section标签获取数据)