简体   繁体   English

如何反序列化接口类型?

[英]How can I deserialize an interface type?

I serialize a class which includes a property called Model as IModel but when I try to Deserialize it I'm getting the following exception: 我序列化一个类,其中包含一个名为Model作为IModel的属性,但是当我尝试反序列化它时,我得到以下异常:

System.Runtime.Serialization.SerializationException: Type 'MYN.IModel' in Assembly 'MYN.Defs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

It's binary serialization. 这是二进制序列化。 Model marked as serializable. Model标记为可序列化。 Obviously IModel is not. 显然IModel不是。

So what's the solution, what am I doing wrong? 那么解决方案是什么,我做错了什么? Why does it try to seriliaze or deserialize an interface anyway? 为什么它试图对接口进行seriliaze或反序列化呢?

PS Interface hasn't got an Enum in it. PS接口没有Enum。

It makes sense to get that error, because an IModel property could refer to different classes and there is no guarantee that they are all Serializable. 获取该错误是有意义的,因为 IModel属性可以引用不同的类,并且不能保证它们都是Serializable。


OK, I gave it a try and we have a: 好的,我试一试,我们有一个:

Testcase error, it works on my computer. 测试用例错误,它可以在我的电脑上运行。


interface IFoo { }

[Serializable]
class CFoo : IFoo   { }

[Serializable]
class Bar
{
    public IFoo Foo { get; set; }
}

And Bar Serializes and Deserializes fine. 和Bar序列化和反序列化很好。

Bar b = new Bar();
b.Foo = new CFoo();

using (var s = new System.IO.MemoryStream())
{
    var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    bf.Serialize(s, b);
    s.Position = 0;
    b = (Bar)bf.Deserialize(s);

    Console.WriteLine("OK");
}

So, what is different from your IModel and Model ? 那么,与您的IModelModel什么不同?

I suspect it's because during deserialization it initiates a new instance of the class and then copies the data into it. 我怀疑这是因为在反序列化期间它启动了一个新的类实例,然后将数据复制到其中。 It can't new an interface so the deserialization can not be completed. 它不能新建接口,因此无法完成反序列化。 This is why you need a constructor that takes no arguments for serialization. 这就是为什么你需要一个不带序列化参数的构造函数。

Not sure on the solution, I've never worked that bit out. 在解决方案上不确定,我从未尝试过这一点。 I'd probably override the class and inherit the property with a concrete type then serialize that. 我可能会覆盖该类并使用具体类型继承该属性,然后序列化该类。

Probably you need to expose those property implicitly within your class: 您可能需要在类中隐式显示这些属性:

instead of IModel.Model property add 而不是IModel.Model属性添加

public MyClass Model

I'm fairly sure that you can't serialize/deserialize interfaces, only instances of classes. 我很确定你不能序列化/反序列化接口,只能是类的实例。 I can't find any documentation to confirm this, but I recall trying to do the same thing without any success. 我找不到任何文件证实这一点,但我记得试图做同样的事情而没有任何成功。

You could try using an abstract base class instead of an interface, but I haven't tried that. 您可以尝试使用抽象基类而不是接口,但我没有尝试过。

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

相关问题 如何将XmlNode []反序列化为T类型? - How can I deserialize an XmlNode[] into type T? 反序列化接口类型 - Deserialize Interface-Type 当事先不知道 object 类型时,如何反序列化 JSON? - How can I deserialize JSON when the object type is not known beforehand? 反序列化JSON-如何反序列化此JSON? - Deserialize JSON - How can I deserialize this JSON? 将类型信息传递给 MongoDB 以便它可以正确反序列化接口类型? - Passing Type information to MongoDB so it can deserialize interface types properly? 使用 Newtonsoft.Json,如何同时使用 SerializationBinder 和 CustomResolver 来反序列化抽象/接口类型? - Using Newtonsoft.Json, how can I use a SerializationBinder and CustomResolver together to deserialize abstract/interface types? 如何使用 Json.Net 将通用接口反序列化为通用具体类型? - How to deserialize generic interface to generic concrete type with Json.Net? 我可以在不引用类型的情况下反序列化泛型吗? - Can I deserialize generics without a reference to the type? 如何将表达式从类型接口转换为特定类型 - How can I cast an expression from type interface, to an specific type 如何反序列化RSS? - How can I Deserialize a RSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM