How to serialize property of type Object with XmlSerializer

前端 未结 3 2003
我寻月下人不归
我寻月下人不归 2020-12-10 17:10

I have a property:

public object Tag

but it can contain finite number of types, unfortunately without base type (except object type). But w

3条回答
  •  轮回少年
    2020-12-10 18:08

    You can also use [XmlInclude(typeof(YourType))] on the class that contains the object property. So in the case of the OP, it would look like this

    [XmlInclude(typeof(PossibleClassOne))]
    [XmlInclude(typeof(PossibleClassTwo))]
    public class MyClass
    {
       public object Tag { get; set; }
    }
    

    This way, you can keep your element name in all cases

提交回复
热议问题