Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class A
  2. {
  3. public int AProp { get; set; }
  4. }
  5.  
  6. public class B : A
  7. {
  8. public int BProp { get; set; }
  9. }
  10.  
  11. public void TestAction(A a)
  12. {
  13. if (!(a is B))
  14. {
  15. throw new Exception("Should be B!");
  16. }
  17. }
  18.  
  19. var setting = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
  20. setting.TypeNameHandling = TypeNameHandling.All;
  21.  
  22. {$type: 'MyNamespace.B, MyAssembly', AProp: 1, BProp: 2}
  23.  
  24. var json = @"{$type: 'MyNamespace.B, MyAssembly', AProp: 1, BProp: 2}";
  25.  
  26. var objA = JsonConvert.DeserializeObject(json, typeof(A), new JsonSerializerSettings
  27. {
  28. TypeNameHandling = TypeNameHandling.None
  29. });
  30.  
  31. var objB = JsonConvert.DeserializeObject(json, typeof(A), new JsonSerializerSettings
  32. {
  33. TypeNameHandling = TypeNameHandling.All
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement