.net 无法在我的自定义.config节中读取
注意:这与此非常相似,但我需要更多帮助 我试图在.config文件中创建以下部分,但在尝试访问此部分时出现异常 .config文件 及 基尔。现在,当我做以下事情时.net 无法在我的自定义.config节中读取,.net,configuration,configurationelement,.net,Configuration,Configurationelement,注意:这与此非常相似,但我需要更多帮助 我试图在.config文件中创建以下部分,但在尝试访问此部分时出现异常 .config文件 及 基尔。现在,当我做以下事情时 var whather=ConfigurationManager.GetSection(“foos”)引发以下异常:- 在创建时出错 的配置节处理程序 foos:类型 'Ackbar.Mvc.Models.Foos.FooCollection' 不继承自 'System.Configuration.IConfigurationSec
var whather=ConfigurationManager.GetSection(“foos”)
引发以下异常:-
在创建时出错
的配置节处理程序
foos:类型
'Ackbar.Mvc.Models.Foos.FooCollection'
不继承自
'System.Configuration.IConfigurationSectionHandler'
有人能帮我吗?我不想将集合包装在父节中
干杯:)您必须实现一个
IConfigurationSectionHandler
。没办法
但是,您也可以让FooCollection
实现该接口
属性属性也可以派上用场。
FooCollection
不是一个节,因此您应该让它扩展ConfigurationSection
尽管如此,您仍然需要创建ConfigurationElementCollection
作为备份集合,您只需要以不同的方式连接它。我会用foosecute
为部分本身命名一些不同的东西
<configSections>
<section name="foos" type="Ackbar.Mvc.Models.Foo.FooSection, Ackbar.Mvc" requirePermission="false"/>
</configSections>
<foos>
<add name="aaa" something="zzz"/>
<add name="bbb" something="yyy"/>
<add name="ccc" something="xxx"/>
</foos>
明白了:)\你终于花了我好长时间。我将在我的开场白中发布一些代码来帮助其他人。干杯,伙计
IConfigurationSectionHandler
自.NET 1.1以来已被弃用。改为使用ConfigurationSection
类,您应该给出一个答案,试图找出对象创建(对象父级、对象配置上下文、XmlNode节)
所期望的是什么,而您只需更改继承的类型,这是浪费时间。
public class FooCollection : ConfigurationElementCollection
{
... with my custom overrides, etc. ...
}
public class FooElement : ConfigurationElement
{
[ConfigurationProperty("Name", IsRequired = true)]
public string Name { .. }
[ConfigurationProperty("Something ", IsRequired = true)]
public string Something { .. }
[ConfigurationProperty("IsDefault ", IsRequired = false, DefaultValue = false)]
public bool IsDefault { .. }
}
<configSections>
<section name="foos" type="Ackbar.Mvc.Models.Foo.FooSection, Ackbar.Mvc" requirePermission="false"/>
</configSections>
<foos>
<add name="aaa" something="zzz"/>
<add name="bbb" something="yyy"/>
<add name="ccc" something="xxx"/>
</foos>
public class FooSection : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection=true)]
public FooCollection Foos => (FooCollection)this[""];
// optionally add convenience accessors to the `Foos` collection
}