I wont to do some serialization with Protobuf-net and getting folowing error for this code snippet:
The error:
Dynamic type is not a contract-type: TestType[]
The snippet:
using System.IO;
namespace QuickStart
{
class Program
{
static void Main()
{
//FileAccess.ShowFileAccess();
//Sockets.ShowSockets();
var dto = new DataTransferType
{
ProtoDynamicProperty = new TestType[]
{
new TestType {UselessProperty="AAA"},
new TestType{UselessProperty="BBB"},
new TestType{UselessProperty="CCC"}
}
};
using (MemoryStream testStream = new MemoryStream())
{
ProtoBuf.Serializer.SerializeWithLengthPrefix(testStream, dto, ProtoBuf.PrefixStyle.Base128);
}
}
}
[ProtoBuf.ProtoContract]
struct TestType
{
[ProtoBuf.ProtoMember(1)]
public string UselessProperty { get; set; }
}
[ProtoBuf.ProtoContract]
class DataTransferType
{
[ProtoBuf.ProtoMember(1, DynamicType = true)]
public object ProtoDynamicProperty { get; set; }
}
}
Any ideas why this happens? I`m using 2.0.0.651 build
Your difficulty is explained (albeit not fully) by the restrictions on
DynamicType
mentioned here at the former project site for protobuf-net:So, what exactly is meant by a contract type? As stated, primitive types are not contract types, but is that all? From Protobuf-net: the unofficial manual: Forms of type serialization in protobuf-net:
So serialization of a "contract type" corresponds to "normal serialization" in this article -- and collections are not contract types. This explains the
Dynamic type is not a contract-type: TestType[]
exception message.As a workaround, you can package your
ProtoDynamicProperty
inside a generic surrogate type that is guaranteed to correspond to a contract type and encapsulates the requisite type information, like so: