Advertisement
Guest User

Untitled

a guest
May 23rd, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  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.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement