I am creating objects dynamically using Activator(C#) and one of these classes looks like:
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
}
When I create instance of Driver class using Activator I get following object:
obj.Item = null;
obj.ItemElementName = "Bit16"
ItemElementName is set by default, because its enum, but how to set Item if its based on this enum? Once again, I am creating many object dynamically with Activator, so I cant hardcode it - its possible to get this information in class and create Item property properly?
Thanks a lot!
ItemElementName
is set toItemChoiceType.Bit16
because that is the first item in the enumeration. Hence its value is0
but you can see it asBit16
. By Activator you creates a new instance. If you don't put arguments in order to set your properties, then their values will be default ones.I see that you have there XmlChoiceIdentifier and other XmlSerializer's stuff. The purpose of this attribute is to:
ItemElementName
property.ItemElementName
after deserialization based on serialized value ofItem
.That's what I can tell you basing on given information...
Here is an example that utilizes XmlSerializer along with XmlChoiceIdentifier: