ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> C#教程 >> Create a custom configSection in webconfig or appconfig file

Create a custom configSection in webconfig or appconfig file(1/2)

来源:网络整理     时间:2015-12-16     关键词:

本篇文章主要介绍了"Create a custom configSection in webconfig or appconfig file",主要涉及到方面的内容,对于C#教程感兴趣的同学可以参考一下: config file:<?xml version="1.0" encoding="utf-8" ?><configuration>&l...

config file:

<?xml version="1.0" encoding="utf-8" ?><configuration><configSections><section name="FileDepend" type="TestConsole.FileDepend,TestConsole"/></configSections><FileDepend><RootDir path="c:\"></RootDir><Public><element file="/1.txt"></element><element file="/2.txt"></element></Public><Modules><module name="legend"><element file="/3.txt"></element><element file="/4.txt"></element></module><module name="bookmark"><element file="/5.txt"></element><element file="/6.txt"></element></module></Modules></FileDepend><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>

FileDepend.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace TestConsole
{
    publicclass FileDepend : ConfigurationSection
    {
        [ConfigurationProperty("RootDir")]
        private RootDirElement _RootDir => (RootDirElement)base["RootDir"];

        [ConfigurationProperty("Public")]
        private FilesCollection PublicFilesCollection => ((FilesCollection)(base["Public"]));

        publicstring RootDir => _RootDir.Name;

        [ConfigurationProperty("Modules")]
        public ModulesCollection ModulesCollection => ((ModulesCollection)(base["Modules"]));

        public IEnumerable<string> PublicFiles => from FileElement v in PublicFilesCollection select v.Name;
    }

    publicclass RootDirElement : ConfigurationElement
    {
        [ConfigurationProperty("path", DefaultValue = "", IsKey = true, IsRequired = true)]
        publicstring Name => (string)base["path"];
    }

    publicclass FileElement : ConfigurationElement
    {
        [ConfigurationProperty("file", DefaultValue = "", IsKey = true, IsRequired = true)]
        publicstring Name => (string)base["file"];
    }
    publicclass ModuleElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
        publicstring Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }
        [ConfigurationProperty("", IsDefaultCollection = true)]
        private FilesCollection Element => (FilesCollection)base[""];

        public IEnumerable<string> Files => from FileElement file in Element select file.Name;
    }
    [ConfigurationCollection(typeof(ModuleElement))]
    publicclass FilesCollection : ConfigurationElementCollection
    {
        internalconststring PropertyName = "element";

        publicoverride ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;

        protectedoverridestring ElementName => PropertyName;

        protectedoverridebool IsElementName(string elementName)
        {
            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
        }


        publicoverridebool IsReadOnly()
        {
            returnfalse;
        }


        protectedoverride ConfigurationElement CreateNewElement()
        {
            returnnew FileElement();
        }

        protectedoverrideobject GetElementKey(ConfigurationElement element)
        {
            return ((FileElement)(element)).Name;
        }

        public FileElement this[int idx] => (FileElement)BaseGet(idx);
        publicnew FileElement this[string idx] => (FileElement)BaseGet(idx);
    }
    [ConfigurationCollection(typeof(ModuleElement))]
    publicclass ModulesCollection : ConfigurationElementCollection
    {
        internalconststring PropertyName = "module";

        publicoverride ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;
        protectedoverridestring ElementName => PropertyName;

        protectedoverridebool IsElementName(string elementName)
        {
            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
        }

        publicoverridebool IsReadOnly()
        {
            returnfalse;
        }

        protectedoverride ConfigurationElement CreateNewElement()
        {
            returnnew ModuleElement();
        }

        protectedoverrideobject GetElementKey(ConfigurationElement element)
        {
            return ((ModuleElement)(element)).Name;
        }

        public ModuleElement this[int idx] => (ModuleElement)BaseGet(idx);
        publicnew ModuleElement this[string idx] => (ModuleElement)BaseGet(idx);
    }
}

run:

相关图片

相关文章