是否有可能具有CollectionElementCollection与许多不同的由式CollectionElements,例如:ConfigurationElementCollection中与许多不同类型的ConfigurationElements的
<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
,但是当我开始我的应用程序我得到未来可预见的错误:
Unrecognized attribute '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()
从不被调用。
所以问题是:如何让孩子们通过自己解决未知元素?
我知道这个答案是前一段时间发布的,但是如果您将此标记为接受的答案,您可以进一步详细了解一下吗? – goric 2012-10-03 21:16:01
@goric:您需要继承它并重写`RetrieveConfigurationElementType`以在运行时替换类型。另请参阅[NameTypeConfigurationElementCollection](http://msdn.microsoft.com/en-us/library/ee939739%28PandP.50%29.aspx) – abatishchev 2012-10-03 22:18:46