Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. using System;
  2. using ServiceStack;
  3. using ServiceStack.Text;
  4.  
  5. public class Parent
  6. {
  7. }
  8.  
  9. public class Child : Parent
  10. {
  11. }
  12.  
  13. var child = new Child();
  14.  
  15. Console.WriteLine("ConvertTo<T> Child To Parent => " + child.ConvertTo<Parent>().GetType().Name);
  16. Console.WriteLine("CastTo<T> Child To Parent => " + child.CastTo<Parent>().GetType().Name);
  17.  
  18. public static T CastTo<T>(this object from)
  19. {
  20. T to = from.ConvertTo<T>();
  21. return to.GetType() == typeof(T)
  22. ? to
  23. : from.ToSafeJson().FromJson<T>();
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement