简体   繁体   English

类型对象的序列化属性

[英]Serialize property of type object

I have a serializable class which contains some properties, including a variable of type object. 我有一个可序列化的类,其中包含一些属性,包括类型为object的变量。 I want my class to be able to contain different (of course serializable) objects. 我希望我的班级能够包含不同的(当然是可序列化的)对象。 The problem is, that I get an exception during serialization: 问题是,在序列化期间出现异常:

Use the XmlInclude or SoapInclude attribute to specify types that are not known statically

I created a small sample for this issue: 我为此问题创建了一个小样本:

class Program
{
    static void Main(string[] args)
    {
        SerializeableTestClass testClass = new SerializeableTestClass();

        testClass.Params = new ExtendedParams();

        MemoryStream ms = new MemoryStream();
        XmlSerializer xmlf = new XmlSerializer(testClass.GetType());
        xmlf.Serialize(ms, testClass);
        ms.Capacity = (int)ms.Length;
        ms.Close();
        byte[] array =  ms.GetBuffer();
    }
}

[Serializable]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]
    public object Params;
}

[Serializable]
public class ParamsBase
{
    [XmlElement(ElementName = "SomeValue")]
    public Int32 SomeValue;
}

[Serializable]
public class ExtendedParams : ParamsBase
{
    [XmlElement(ElementName = "SomeNewValue")]
    public Int32 SomeNewValue;
}

Is there a possibility to serialize and deserialize this class without specifying the concrete type of "Params" ??? 是否有可能在不指定“ Params”具体类型的情况下序列化和反序列化此类?

Best regards 最好的祝福

Is it acceptable to include all the possible types on the class that is serialized? 在序列化的类中包括所有可能的类型是否可以接受? (That is what the message means, that no type info is included for ExtendedParams ). (这就是消息的含义, ExtendedParams不包含类型信息)。

You can include it in the XmlSerializeritself , or include it on the main class: 您可以将其包含在XmlSerializeritself ,也可以将其包含在主类中:

[Serializable]
[XmlInclude(typeof(ParamsBase))]
[XmlInclude(typeof(ExtendedParams))]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]    
    public object Params;
}

But to dynamically serialize if you don't know all the types: 但是,如果您不知道所有类型,则要进行动态序列化:

//static: XmlSerializer xmlf = new XmlSerializer(testClass.GetType(),new Type[]{typeof(ExtendedParams)});

//dynamic:
Type[] extratypes = testClass.Params == null ? null : new Type[] { testClass.Params.GetType() };
XmlSerializer xmlf = new XmlSerializer(testClass.GetType(), extratypes );

modify your code , when you initialize the XmlSerializer 初始化XmlSerializer时修改您的代码

 XmlSerializer xmlf = new XmlSerializer(testClass.GetType(),new Type [] {typeof(ExtendedParams)});

this will let the XmlSerializer know about the other types in the class 这将使XmlSerializer知道该类中的其他类型

As the exception message suggests, you need to specify the derived type so that it can be recognized: 如异常消息所示,您需要指定派生类型,以便可以识别它:

[Serializable]
[XmlInclude(typeof(ExtendedParams))]
public class SerializeableTestClass
{
    [XmlElement(ElementName = "Params")]
    public object Params;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM