2
我有一些使用Protobuf.net的相當簡單的代碼,它引發了一個非常奇怪的異常。 在第167行的MetaType.cs中,它引發了一個InvalidOperationException「一旦生成序列化程序,就不能更改該類型」。這是什麼意思,我該如何解決?Protobuf.net「一旦生成序列化程序就無法更改類型」
我的代碼如下所示:
這種方法開始關閉所有的串行化:
while (!Parallel.For(0, 100, (i) =>
{
Widget w;
lock (f) { w = f.CreateWidget(); }
SerialiseWidget(w);
}).IsCompleted)
{
Thread.Sleep(10);
}
很簡單,它只是通過一切環路並行,串行化和部件100。
的串行化處理方法也很簡單:
private byte[] SerialiseWidget(Widget w)
{
using (MemoryStream m = new MemoryStream())
{
Serializer.Serialize<PacketChunk>(m, w);
return m.ToArray();
}
}
最後,部件類看起來是這樣的:
[ProtoContract]
private class Widget
{
[ProtoMember(1)]
public int a;
[ProtoMember(2)]
public byte[] b;
[ProtoMember(3)]
public Thing c; //Thing is itself a protocontract
[ProtoMember(4)]
public int d;
[ProtoMember(5)]
public int e;
}
編輯::我懷疑這可能是與事實做我並行循環。線程安全是Protobuf.net的那種東西?
非常好,調用了PrepareSerializer方法。感謝Marc:D的快速反應 – Martin 2010-10-23 23:10:05