的未知線型異常我已經開始使用Protobuf-net將Unity/iOS遊戲轉換爲保存狀態。它看起來像事情工作正常,直到我說這個實例變量GameState
使用Protobuf-net,我得到了一個有關列表<Subclassable>
[ProtoMember(10)]
public List<Unit> fUnits;
單位是
[ProtoContract]
[ProtoInclude(21, typeof(ArtilleryUnit))]
[ProtoInclude(22, typeof(CavalryArtilleryUnit))]
[ProtoInclude(23, typeof(CavalryUnit))]
[ProtoInclude(24, typeof(InfantryUnit))]
[Serializable]
public class Unit : IActionHandler
這是第一個子類,我已經系列化。我被反序列化寫作
using (Stream memoryStream = new MemoryStream()) {
byte[] data = System.Text.Encoding.UTF8.GetBytes(readText);
memoryStream.Write(data, 0, data.Length);
memoryStream.Position = 0;
WBTSSerializer deserializer = new WBTSSerializer();
testState = new GameState();
deserializer.Deserialize(memoryStream, testState, typeof(GameState));
}
後立即測試我的代碼,我得到
ProtoBuf.ProtoException: Invalid wire-type; this usually means you have over-written a file without truncating or setting the length; see http://stackoverflow.com/q/2152978/23354
at ProtoBuf.ProtoReader.SkipField() [0x00000] in <filename unknown>:0
at WBTSSerializer.Read (wbts.Unit , ProtoBuf.ProtoReader) [0x00000] in <filename unknown>:0
at WBTSSerializer.Read (wbts.GameState , ProtoBuf.ProtoReader) [0x00000] in <filename unknown>:0
at WBTSSerializer.Deserialize (Int32 , System.Object , ProtoBuf.ProtoReader) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.DeserializeCore (ProtoBuf.ProtoReader reader, System.Type type, System.Object value, Boolean noAutoCreate) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type) [0x00000] in <filename unknown>:0
at wbts.Game.SaveGame (System.String filename) [0x000c1] in /Users/david/Shenandoah/WBTS/wbts/wbts/Game.cs:620
不用說了,我已經檢查覆蓋(這是一個全新的文件)。而且,如果我拿出ProtoMember(10)
,它不會拋出。
如果我拿出ProtoInclude
行,我得到關於子類的錯誤。而且,如果我在Unit
內取出ProtoMember
,它仍然會拋出。
如果很重要,我現在只是從Xamarin運行這個(在Mac OS X上 - 我是一個iOS開發人員)。但是這只是爲了便於測試,我已經設法用足夠的支持來使Protobuf-net在iPad上運行。
這就是我們的XML序列化中一個.NET無知的複製粘貼。不幸的是,一旦我使用了適當的byte []處理(包括Mono本身崩潰),我就開始發現其他錯誤,然後我忘記了通過獲取支持iOS的DLL的步驟。所以現在我回到.NET的XML序列化。 –