【发布时间】:2013-07-13 10:18:06
【问题描述】:
我不会使用 Protobuf-net 进行一些序列化,并且此代码 sn-p 会出现以下错误:
错误:
动态类型不是合约类型:TestType[]
sn-p:
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; }
}
}
任何想法为什么会发生这种情况?我正在使用 2.0.0.651 构建
【问题讨论】:
-
刚刚回答了this question,我意识到我也知道这个老问题的答案,所以我继续回答它,尽管它已经很老了。
标签: object serialization protobuf-net dynamictype