<root name="myRoot" type="roottype"> <element name="myname" type="myType" /> <element name="hisname" type="hisType" /> <element name="yourname" type="yourType" /> </root>
而不是这样:
<root name="myRoot" type="roottype"> <elements> <element name="myname" type="myType" /> <element name="hisname" type="hisType" /> <element name="yourname" type="yourType" /> </elements> </root>解决方法 要使您的集合元素直接位于父元素(而不是子集合元素)中,则需要重新定义ConfigurationProperty.例如,我有一个收集元素,如:
public class TestConfigurationElement : ConfigurationElement{ [ConfigurationProperty("name",IsKey = true,Isrequired = true)] public string name { get { return (string)this["name"]; } }}
并收集如:
[ConfigurationCollection(typeof(TestConfigurationElement),AddItemname = "test")]public class TestConfigurationElementCollection : ConfigurationElementCollection{ protected overrIDe ConfigurationElement CreateNewElement() { return new TestConfigurationElement(); } protected overrIDe object GetElementKey(ConfigurationElement element) { return ((TestConfigurationElement)element).name; }}
我需要将父节/元素定义为:
public class TestConfigurationSection : ConfigurationSection{ [ConfigurationProperty("",IsDefaultCollection = true)] public TestConfigurationElementCollection Tests { get { return (TestConfigurationElementCollection)this[""]; } }}
请注意[ConfigurationProperty(“”,IsDefaultCollection = true)]属性.给它一个空的名称,并将其设置为默认集合允许我定义我的配置,如:
<testConfig> <test name="One" /> <test name="Two" /></testConfig>
代替:
<testConfig> <tests> <test name="One" /> <test name="Two" /> </tests></testConfig>总结
以上是内存溢出为你收集整理的c# – 如何从App.config中读取此自定义配置?全部内容,希望文章能够帮你解决c# – 如何从App.config中读取此自定义配置?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)