0

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

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
  • (noticed it after answering...) possible duplicate of [Deserializing polymorphic json classes without type information using json.net](http://stackoverflow.com/questions/19307752/deserializing-polymorphic-json-classes-without-type-information-using-json-net) – Alex Mar 05 '14 at 11:20

1 Answers1

1

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.

Alex
  • 23,004
  • 4
  • 39
  • 73
  • I agree with you , how ever I cannot cast member to type B, because after JSON deserialization member is of type A losing all type B data. – Shachaf.Gortler Mar 05 '14 at 11:12
  • I just stumbled across a potential dupe question which is probably what you're looking for. See my comment under the question. – Alex Mar 05 '14 at 11:19