Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [DataContract, Serializable, KnownType(typeof(B))]
- public class A
- {
- [DataMember]
- int x = 42;
- }
- [DataContract, Serializable]
- public class B : A
- {
- [DataMember]
- int y = 24;
- }
- class Program
- {
- public static string SerialiseAs<TResult>(TResult input)
- {
- DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(TResult));
- MemoryStream stream = new MemoryStream();
- ser.WriteObject(stream, input);
- stream.Position = 0;
- StreamReader reader = new StreamReader(stream);
- return reader.ReadToEnd();
- }
- static void Main(string[] args)
- {
- Console.WriteLine(SerialiseAs<A>(new B()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement