... protobuf-net . :
[ProtoInclude(typeof(Foo), 20)]
[ProtoInclude(typeof(Bar), 21)]
public abstract class MyBase {
public byte[] GetBytes()
{
using(MemoryStream ms = new MemoryStream())
{
Serializer.Serialize<MyBase>(ms, this);
return ms.ToArray();
}
}
}
[ProtoContract]
class Foo : MyBase { }
[ProtoContract]
class Bar : MyBase { }
. () ; Serializer.Serialize<Foo>(stream, obj), , , , , , MyBase. () , Deserialize MyBase , Foo Bar .
, :
Serializer.Serialize<BaseType>(dest, obj);
...
BaseType obj = Serializer.Deserialize<BaseType>(source);
Serializer.Serialize<DerivedType>(dest, obj);
...
DerivedType obj = Serializer.Deserialize<DerivedType>(source);
, .