class A { public int Foo { get; set; } public int Bar { get; set; } public A() { } public A(A a) { Foo = a.Foo; Bar = a.Bar; } } class B : A { public int Hello { get; set; } public B() : base() { } public B(A a) : base(a) { } }
EDIT
If you do not want to copy each property, you can serialize A and B and serialize your instance of A (let's say Stream, not a file) and initialize your instance of B with it. But I warn you, this is disgusting and has quite a bit of overhead:
A a = new A() { Bar = 1, Foo = 3 }; System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(A)); System.IO.Stream s = new System.IO.MemoryStream(); xs.Serialize(s, a); string serial = System.Text.ASCIIEncoding.ASCII.GetString(ReadToEnd(s)); serial = serial.Replace("<A xmlns", "<B xmlns"); serial = serial.Replace("</A>", "</B>"); s.SetLength(0); byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(serial); s.Write(bytes, 0, bytes.Length); s.Position = 0; xs = new System.Xml.Serialization.XmlSerializer(typeof(B)); B b = xs.Deserialize(s) as B;
You can get more information about ReadToEnd here .
source share