using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace ConsoleApplication2
public static void Main(string[] args)
var a = new SomeOtherClass();
a.MyProperty.Bar = "this is bar";
a.MyProperty.Foo = "this is foo";
a.MyProperty.AnotherProperty = "another property";
string encoded = a.MyProperty.SerializeBase64();
Console.WriteLine(encoded);
var b = MyAbstractBase.DeserializeBase64(encoded);
Console.WriteLine(b.Bar);
Console.WriteLine(b.Foo);
var c = b as SomeOtherClass.MyDerivedClass;
Console.WriteLine(c.AnotherProperty);
[KnownType(typeof(SomeOtherClass.MyDerivedClass))]
public abstract class MyAbstractBase
public string Foo { get; set; }
public abstract string Bar { get; set; }
public string SerializeBase64()
MemoryStream ws = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(this.GetType(), new List<Type>() { typeof(SomeOtherClass.MyDerivedClass) });
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ws);
serializer.WriteObject(binaryDictionaryWriter, this);
binaryDictionaryWriter.Flush();
string encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
public static MyAbstractBase DeserializeBase64(string s)
int length = Convert.ToInt32(s.Substring(0, p));
byte[] memorydata = Convert.FromBase64String(s.Substring(p + 1));
MemoryStream rs = new MemoryStream(memorydata, 0, length);
DataContractSerializer serializer = new DataContractSerializer(typeof(MyAbstractBase), new List<Type>() { typeof(SomeOtherClass.MyDerivedClass) });
XmlDictionaryReader binaryDictionaryReader = XmlDictionaryReader.CreateBinaryReader(rs, XmlDictionaryReaderQuotas.Max);
return (MyAbstractBase)serializer.ReadObject(binaryDictionaryReader);
public class SomeOtherClass
public MyDerivedClass MyProperty { get; set; }
MyProperty = new MyDerivedClass();
public class MyDerivedClass : MyAbstractBase
public override string Bar { get; set; }
public string AnotherProperty { get; set; }