基于XmlChoiceIdentifier创建对象 [英] Create object based on XmlChoiceIdentifier
问题描述
我正在使用Activator(C#)动态创建对象,这些类之一如下:
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
}
当我使用激活器创建Driver类的实例时,得到以下对象:
When I create instance of Driver class using Activator I get following object:
obj.Item = null;
obj.ItemElementName = "Bit16"
ItemElementName默认设置,因为其枚举,但是如果基于此枚举设置Item,该如何设置?
再一次,我使用Activator动态创建了许多对象,因此我无法对其进行硬编码-是否有可能在类中获取此信息并正确创建Item属性?
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?
非常感谢!
推荐答案
ItemElementName
设置为 ItemChoiceType.Bit16
,因为这是枚举中的第一项。因此,它的值是 0
,但是您可以将其视为 Bit16
。通过激活器,您可以创建一个新实例。如果您不输入参数来设置属性,那么它们的值将是默认值。
ItemElementName
is set to ItemChoiceType.Bit16
because that is the first item in the enumeration. Hence its value is 0
but you can see it as Bit16
. 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.
我看到您那里有XmlChoiceIdentifier和其他XmlSerializer的东西。此属性的用途是:
I see that you have there XmlChoiceIdentifier and other XmlSerializer's stuff. The purpose of this attribute is to:
- 请勿序列化
ItemElementName
属性。 / li>
- 要根据
Item
的序列化值在反序列化后恢复ItemElementName
。
- Do not serialize
ItemElementName
property. - To restore
ItemElementName
after deserialization based on serialized value ofItem
.
这就是我可以根据给定的信息告诉您的信息...
That's what I can tell you basing on given information...
这里是一个使用XmlSerializer和XmlChoiceIdentifier的示例:
Here is an example that utilizes XmlSerializer along with XmlChoiceIdentifier:
public class Choices
{
[XmlChoiceIdentifier("ItemType")]
[XmlElement("Text", Type = typeof(string))]
[XmlElement("Integer", Type = typeof(int))]
[XmlElement("LongText", Type = typeof(string))]
public object Choice { get; set; }
[XmlIgnore]
public ItemChoiceType ItemType;
}
[XmlType(IncludeInSchema = false)]
public enum ItemChoiceType
{
Text,
Integer,
LongText
}
class Program
{
static void Main(string[] args)
{
Choices c1 = new Choices();
c1.Choice = "very long text"; // You can put here a value of String or Int32.
c1.ItemType = ItemChoiceType.LongText; // Set the value so that its type match the Choice type (Text or LongText due to type of value is string).
var serializer = new XmlSerializer(typeof(Choices));
using (var stream = new FileStream("Choices.xml", FileMode.Create))
serializer.Serialize(stream, c1);
// Produced xml file.
// Notice:
// 1. LongText as element name
// 2. Choice value inside the element
// 3. ItemType value is not stored
/*
<?xml version="1.0"?>
<Choices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LongText>very long text</LongText>
</Choices>
*/
Choices c2;
using (var stream = new FileStream("Choices.xml", FileMode.Open))
c2 = (Choices)serializer.Deserialize(stream);
// c2.ItemType is restored
}
}
这篇关于基于XmlChoiceIdentifier创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!