非常に奇妙な例外をスローしている Protobuf.net を使用したかなり単純なコードがあります。MetaType.cs の 167 行目で、「シリアライザーが生成されると型を変更できません」という InvalidOperationException がスローされます。これは何を意味し、どうすれば修正できますか?
私のコードは次のようになります。
このメソッドは、すべてのシリアライズを開始します。
while (!Parallel.For(0, 100, (i) =>
{
Widget w;
lock (f) { w = f.CreateWidget(); }
SerialiseWidget(w);
}).IsCompleted)
{
Thread.Sleep(10);
}
とても単純です。Parallel ですべてをループし、100 個のウィジェットをシリアル化します。
serialise メソッドも非常に単純です。
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 はどの程度スレッドセーフなのでしょうか?