国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CreateacustomconfigSectioninweb.configorapp.configfile

2019-11-14 13:40:45
字體:
來源:轉載
供稿:網友

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{    public class FileDepend : ConfigurationSection    {        [ConfigurationPRoperty("RootDir")]        private RootDirElement _RootDir => (RootDirElement)base["RootDir"];        [ConfigurationProperty("Public")]        private FilesCollection PublicFilesCollection => ((FilesCollection)(base["Public"]));        public string RootDir => _RootDir.Name;        [ConfigurationProperty("Modules")]        public ModulesCollection ModulesCollection => ((ModulesCollection)(base["Modules"]));        public IEnumerable<string> PublicFiles => from FileElement v in PublicFilesCollection select v.Name;    }    public class RootDirElement : ConfigurationElement    {        [ConfigurationProperty("path", DefaultValue = "", IsKey = true, IsRequired = true)]        public string Name => (string)base["path"];    }    public class FileElement : ConfigurationElement    {        [ConfigurationProperty("file", DefaultValue = "", IsKey = true, IsRequired = true)]        public string Name => (string)base["file"];    }    public class ModuleElement : ConfigurationElement    {        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]        public string 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))]    public class FilesCollection : ConfigurationElementCollection    {        internal const string PropertyName = "element";        public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;        protected override string ElementName => PropertyName;        protected override bool IsElementName(string elementName)        {            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);        }        public override bool IsReadOnly()        {            return false;        }        protected override ConfigurationElement CreateNewElement()        {            return new FileElement();        }        protected override object GetElementKey(ConfigurationElement element)        {            return ((FileElement)(element)).Name;        }        public FileElement this[int idx] => (FileElement)BaseGet(idx);        public new FileElement this[string idx] => (FileElement)BaseGet(idx);    }    [ConfigurationCollection(typeof(ModuleElement))]    public class ModulesCollection : ConfigurationElementCollection    {        internal const string PropertyName = "module";        public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;        protected override string ElementName => PropertyName;        protected override bool IsElementName(string elementName)        {            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);        }        public override bool IsReadOnly()        {            return false;        }        protected override ConfigurationElement CreateNewElement()        {            return new ModuleElement();        }        protected override object GetElementKey(ConfigurationElement element)        {            return ((ModuleElement)(element)).Name;        }        public ModuleElement this[int idx] => (ModuleElement)BaseGet(idx);        public new ModuleElement this[string idx] => (ModuleElement)BaseGet(idx);    }}

 

run:

static void Main(string[] args)        {            var v = ConfigurationManager.GetSection("FileDepend") as FileDepend;            var rootDir = v.RootDir;            var publicFiles = v.PublicFiles;            var legendFiles = v.ModulesCollection["legend"].Files;            Console.WriteLine(rootDir);            publicFiles.ToList().ForEach(Console.WriteLine);            legendFiles.ToList().ForEach(Console.WriteLine);            Console.ReadLine();        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清水河县| 博罗县| 墨竹工卡县| 潢川县| 保亭| 屏南县| 通辽市| 安阳县| 盐源县| 鱼台县| 二手房| 洪泽县| 云梦县| 介休市| 江门市| 武隆县| 舟曲县| 康马县| 四子王旗| 进贤县| 绵竹市| 获嘉县| 迁安市| 靖边县| 阜阳市| 五峰| 宜宾县| 淄博市| 广宗县| 清镇市| 海阳市| 铜川市| 榆树市| 伊川县| 盐边县| 平阴县| 鹤岗市| 托克逊县| 曲沃县| 裕民县| 古田县|