当前位置: 移动技术网 > IT编程>开发语言>.net > 配置文件_自定义section标签获取数据

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

2019年06月19日  | 移动技术网IT编程  | 我要评论

滑头鬼之孙高清壁纸,全城热恋20130127,勇敢者游戏2

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

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;
            }
        }
    }
}

 

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网