简体   繁体   English

序列化C#对象,想要反序列化到接口

[英]Serialize C# object, want to deserialize to an interface

I want to send instruction objects between a server and a client. 我想在服务器和客户端之间发送指令对象。 I want to be able to serialize an implementation of an interface and then, when I deserialize it, I want to deserialize it to the generic interface type (while it maintains the actual type it was serialized as). 我希望能够序列化接口的实现,然后,当我反序列化它时,我想将它反序列化为通用接口类型(同时它保持它被序列化的实际类型)。

I tried doing this with the default BinarySerializer but I encountered problems when sending the information across the wire. 我尝试使用默认的BinarySerializer执行此操作,但是在通过线路发送信息时遇到了问题。

I also tried using Protobuf to serialize but with Protobuf you must know the fully qualified type of the object you want to deserialize to. 我也尝试使用Protobuf进行序列化,但是使用Protobuf,您必须知道要反序列化的对象的完全限定类型。 IE: IE:

public interface IExample
{
     void foo();
}
public class Implementation : IExample
{
     void foo() { Console.WriteLine("This is an implementation!"); }
}

can only be deserialized with: 只能通过以下方式反序列化:

Serializer.Deserialize<Implementation>(stream); 

which defeats the point of polymorphism. 这打破了多态性的观点。

Is there a serialization library I can use where the caller of the deserialize method doesn't need to know the object's fully qualified type? 是否有一个序列化库我可以使用deserialize方法的调用者不需要知道对象的完全限定类型?

IE: IE:

Implementation imp = new Implementation();
Stream inStream = Serialize(implementation);
IExample example = Deserialize(inStream);

example.foo();

This should print "This is an implementation!" 这应该打印“这是一个实现!” to the console. 到控制台。

JSON is what I would recommend. JSON是我推荐的。 Of course if you want to parse it as a type, you always need to know a type, but the JSON serializer is the best for what I think you need. 当然,如果你想将它解析为一个类型,你总是需要知道一个类型,但JSON序列化器是我认为你需要的最好的。

I would recommend using the DataContractJsonSerializer in System.Runtime.Serialization.Json as it is the most flexible in my experience. 我建议在System.Runtime.Serialization.Json中使用DataContractJsonSerializer,因为它是我体验中最灵活的。

Here is some additional helpful questions/articles about emitting and deserializing to types: when does DataContractJsonSerializer include the type information? 以下是有关向类型发出和反序列化的一些其他有用的问题/文章: DataContractJsonSerializer何时包含类型信息?

http://kashfarooq.wordpress.com/2011/01/31/creating-net-objects-from-json-using-datacontractjsonserializer/ http://kashfarooq.wordpress.com/2011/01/31/creating-net-objects-from-json-using-datacontractjsonserializer/

You are wanting to walk on shifting sands, which can be hard to manage in strictly typed languages. 你想要在流动的沙滩上行走,这种沙子很难用严格的类型语言来管理。

If you have an object, try the approach of using the as keyword: 如果您有对象,请尝试使用as关键字的方法:

var example = Desrialize(inStream) as IExample;
if (example != null)
    example.foo();

The method I would use though would be to create a particular base object and implement IExample on that base, then when deserializing any object that extends it you can still cast them as the base object or as IExample . 我将使用的方法是创建一个特定的基础对象并在该基础上实现IExample,然后在反序列化任何扩展它的对象时,您仍然可以将它们转换为基础对象或IExample Alternatively, consider whether the polymorphism should take place in the Deserialize() method, either by overloading or by using generics. 或者,考虑是否应该在Deserialize()方法中进行多态,通过重载或使用泛型。

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

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