【发布时间】:2012-01-10 21:38:30
【问题描述】:
是否可以有一个 CollectionElementCollection,其中包含许多不同类型的 CollectionElement,例如:
<collection>
<add type="MyType1, MyLib" Type1SpecificProp="1" />
<add type="MyType2, MyLib" Type2SpecificProp="2" />
</collection
我拥有此类解决方案所需的所有课程:
class MyCollection : ConfigurationElementCollection { }
class MyElement : ConfigurationElement { }
class MyType1 : MyElement { }
class MyType2 : MyElement { }
...
etc
但是当我启动我的应用程序时,我得到了下一个可预测的错误:
无法识别的属性“Type1SpecificProp”。
因为Type1SpecificProp 是在MyType1 而不是MyElement 中定义的,特别是如果MyCollection 有下一个方法:
protected override ConfigurationElement CreateNewElement()
{
return new MyElement(); // but I want instantiate not the base class but by a type given
}
即返回基类,因此子类中的 OnDeserializeUnrecognizedAttribute() 永远不会被调用。
那么问题来了:如何让子类自行解析未知元素?
【问题讨论】:
标签: c# .net app-config system.configuration custom-configuration