【发布时间】:2013-05-26 16:22:18
【问题描述】:
我正在使用 Activator(C#) 动态创建对象,其中一个类如下所示:
class Driver
{
Driver() { }
[XmlChoiceIdentifier("ItemElementName")]
[XmlElement("Bit16", typeof(DriverModule))]
[XmlElement("Bit32", typeof(DriverModule))]
[XmlElement("Bit64", typeof(DriverModule))]
[XmlElement("Unified", typeof(DriverUnified))]
public object Item { get; set; }
[XmlIgnore]
public ItemChoiceType ItemElementName { get; set; }
// ... other serialization methods
}
当我使用 Activator 创建 Driver 类的实例时,我得到以下对象:
obj.Item = null;
obj.ItemElementName = "Bit16"
ItemElementName是默认设置的,因为它的枚举,但是如果它基于这个枚举怎么设置Item呢? 再一次,我正在使用 Activator 动态创建许多对象,所以我无法对其进行硬编码 - 可以在类中获取此信息并正确创建 Item 属性吗?
非常感谢!
【问题讨论】:
标签: c# reflection xml-serialization activator