简体   繁体   中英

SignalR send object from client to server is not deserialized correctly

I'm using signalR 2.0.2

I have an object in my client

 Class A {}
 Class B : A {}
 Class C : { public A member {get;set;}

 C obj = new C();
 obj.member = new B();

In both client and server I have the same json serializer option off TypeNameHandling = TypeNameHandling.Auto

when i send the object to the server it get there , however the member is show as of type A , when it should b type B. I use the TraceWriter log and both on the client side and server side. the $type attirubte in the JSON shows the object type is B

That's because member is A (the class definition says so). You can put a B there because B can be implicitly cast to A (since it inherits A ), but content doesn't change its type (which will always be A ).

It's the same that happens when we use generic collections like this:

IEnumerable<int> temp = new List<int>();
IEnumerable<int> temp2 = new HashSet<int>();

Both temp and temp2 are IEnumerable<int> no matter what we assign to them (both List<> and HashSet<> implement IEnumerable<> ).

To make it so member is recognized as type B , you have to cast it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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