This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 23rd, 2012  |  syntax: C#  |  size: 0.81 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     [DataContract, Serializable, KnownType(typeof(B))]
  2.     public class A
  3.     {
  4.         [DataMember]
  5.         int x = 42;
  6.     }
  7.  
  8.     [DataContract, Serializable]
  9.     public class B : A
  10.     {
  11.         [DataMember]
  12.         int y = 24;
  13.     }
  14.  
  15.  
  16.     class Program
  17.     {
  18.  
  19.         public static string SerialiseAs<TResult>(TResult input)
  20.         {
  21.             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(TResult));
  22.             MemoryStream stream = new MemoryStream();
  23.             ser.WriteObject(stream, input);
  24.             stream.Position = 0;
  25.             StreamReader reader = new StreamReader(stream);
  26.             return reader.ReadToEnd();
  27.         }
  28.  
  29.         static void Main(string[] args)
  30.         {
  31.             Console.WriteLine(SerialiseAs<A>(new B()));
  32.         }
  33.     }
clone this paste RAW Paste Data