Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface MyInterface
- {
- int Property1 { get; set; }
- int Property2 { get; set; }
- }
- [Serializable]
- [DataContract]
- public MyObject1 : MyInterface
- {
- [DataMember(Name = "p1")
- public int Property1 { get; set; }
- [DataMember(Name = "p2")
- public int Property2 { get; set; }
- public MyObject1() {}
- public MyObject1(int a, int b) { this.Property1 = a; this.Property2 = b; }
- }
- public MyObject2, ISerializable
- {
- public string P1 { get; set; }
- public MyInterface M1 { get; set; }
- public MyObject2() {}
- public MyObject2(SerializationInfo info, StreamingContext context) {}
- public void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- info.AddValue("p1", this.P1);
- info.AddValue("m1", this.M1);
- }
- }
- //--------
- // Code to Serialize looks like this:
- MyObject2 obj = new MyObject2();
- obj1.p1 = 1;
- obj1.m1 = new MyObject1(1, 2);
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- MemoryStream ms = new MemoryStream();
- serializer.WriteObject(ms, obj);
- return Encoding.Default.GetString(ms.ToArray());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement