ConfigurationElementCollection与许多不同类型的ConfigurationElements的 [英] ConfigurationElementCollection with a number of ConfigurationElements of different type
问题描述
是否有可能有一些不同的按照类型CollectionElements,例如,一个CollectionElementCollection:
Is it possible to have a CollectionElementCollection with a number of different by type CollectionElements, e.g.:
<collection>
<add type="MyType1, MyLib" Type1SpecificProp="1" />
<add type="MyType2, MyLib" Type2SpecificProp="2" />
</collection
我也需要这样的解决方案的所有类:
I have all classes required for such solution:
class MyCollection : ConfigurationElementCollection { }
class MyElement : ConfigurationElement { }
class MyType1 : MyElement { }
class MyType2 : MyElement { }
...
etc
但是当我开始我的申请,我得到下一个predictable错误:
but when I start my application I'm getting next predictable error:
无法识别的属性Type1SpecificProp。
Unrecognized attribute 'Type1SpecificProp'.
由于 Type1SpecificProp
在 MyType1
定义不是 MyElement
,特别是如果 MyCollection的
有下一个方法:
because Type1SpecificProp
is defined in MyType1
not MyElement
, especially if MyCollection
has next method:
protected override ConfigurationElement CreateNewElement()
{
return new MyElement(); // but I want instantiate not the base class but by a type given
}
即。返回基类从而 OnDeserializeUnrecognizedAttribute()
儿童归类永远不会被调用。
i.e. returns base class thus OnDeserializeUnrecognizedAttribute()
in child classed are never been called.
所以,问题是:如何让子类由他们自行解决未知的元素
So the question is: how to let child classes to resolve unknown elements by their self?
推荐答案
<$c$c>Microsoft.Practices.EnterpriseLibrary.Common.Configuration.PolymorphicConfigurationElementCollection<T>$c$c>从EntLib5做好这项工作作为一个魅力。
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.PolymorphicConfigurationElementCollection<T>
from EntLib5 do this job as a charm.
这篇关于ConfigurationElementCollection与许多不同类型的ConfigurationElements的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!