欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#创建自定义配置节

程序员文章站 2023-11-10 21:59:58
name为自定义节点的名称,type为自定义节点解析文件的命名空间和自定义节处理程序的类名 1. 在节点中注册你所要定义的节点名称及用于处理该节点的配置节处理程序。代码如下:

name为自定义节点的名称,type为自定义节点解析文件的命名空间和自定义节处理程序的类名

1. 在<configsections>节点中注册你所要定义的节点名称及用于处理该节点的配置节处理程序。代码如下:

<configsections>
<section name="dbfactory" type="dbfactory.configuration.dbfactorysection,dbfactory.configuration"/>
</configsections>

2. 在适当的位置添加自定义的节点。代码如下:

  <configsections>
        <section name="dbfactory" type="dbfactory.configuration.dbfactorysection,dbfactory.configuration"</configsections>

    <dbfactory>
        <default factory="sql"></default>
        <factorys>
            <add name="sql"  assembly="hello.data"  class="sqldbfactory" />
            <add name="oracle" assembly="hello.data" class="oracledbfactory" />             
        </factorys>
    </dbfactory>

 public class defaultelement : configurationelement
    {
        [configurationproperty("factory")]
        public string factory
        {
            get
            {
                return this["factory"] as string;
            }
            set
            {
                this["factory"] = value;
            }
        }
    }

public class factoryelement : configurationelement
    {
        [configurationproperty("name")]
        public string name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        [configurationproperty("assembly")]
        public string assembly
        {
            get
            {
                return this["assembly"] as string;
            }
            set
            {
                this["assembly"] = value;
            }
        }

        [configurationproperty("class")]
        public string class
        {
            get
            {
                return this["class"] as string;
            }
            set
            {
                this["class"] = value;
            }
        }
    }
public class factoryelements : configurationelementcollection
    {
        protected override configurationelement createnewelement()
        {
            return new factoryelement();
        }

        protected override object getelementkey(configurationelement element)
        {
            return ((factoryelement)element).name;
        }

        public factoryelement this[string name]
        {
            get
            {
                return baseget(name) as factoryelement;
            }
        }
    }

public class dbfactorysection : configurationsection
    {
        [configurationproperty("default")]
        public defaultelement defaultfactory
        {
            get
            {
                return this["default"] as defaultelement;
            }
            set
            {
                this["default"] = value;
            }
        }

        [configurationproperty("factorys")]
        public factoryelements factorys
        {
            get
            {
                return this["factorys"] as factoryelements;
            }

            set
            {
                this["factorys"] = value;
            }
        }
    }

原文链接: