我在反序列化时遇到了以下错误:
无效的线路类型;这通常意味着您已经覆盖了一个文件而没有截断或设置长度。请参阅使用Protobuf-net,我突然遇到了有关未知线路类型的异常 上述链接只提到了文件截断问题,但是我正在创建一个新文件。
无效的线路类型;这通常意味着您已经覆盖了一个文件而没有截断或设置长度。请参阅使用Protobuf-net,我突然遇到了有关未知线路类型的异常 上述链接只提到了文件截断问题,但是我正在创建一个新文件。
Stopwatch sw = new Stopwatch();
List<SomeClass> items = CreateSomeClass();
sw.Start();
using (var file = File.Create(fileName))
{
var model = CreateModel();
model.Serialize(file, items);
file.SetLength(file.Position);
}
sw.Stop();
logger.Debug("Saving/serialization to {0} took {1} m/s", fileName, sw.ElapsedMilliseconds);
sw.Reset();
logger.Debug("Starting deserialzation...");
sw.Start();
using (var returnStream = new FileStream(fileName, FileMode.Open))
{
var model = CreateModel();
var deserialized = model.Deserialize(returnStream, null, typeof(SomeClass));
}
logger.Debug("Retrieving/deserialization of {0} took {1} m/s", fileName, sw.ElapsedMilliseconds);
public static TypeModel CreateModel()
{
RuntimeTypeModel model = TypeModel.Create();
model.Add(typeof(SomeClass), false)
.Add(1, "SomeClassId")
.Add(2, "FEnum")
.Add(3, "AEnum")
.Add(4, "Thing")
.Add(5, "FirstAmount")
.Add(6, "SecondAmount")
.Add(7, "SomeDate");
TypeModel compiled = model.Compile();
return compiled;
}
public enum FirstEnum
{
First = 0,
Second,
Third
}
public enum AnotherEnum
{
AE1 = 0,
AE2,
AE3
}
[Serializable()]
public class SomeClass
{
public int SomeClassId { get; set; }
public FirstEnum FEnum { get; set; }
public AnotherEnum AEnum { get; set; }
string thing;
public string Thing
{
get{return thing;}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("Thing");
thing = value;
}
}
public decimal FirstAmount { get; set; }
public decimal SecondAmount { get; set; }
public decimal ThirdAmount { get { return FirstAmount - SecondAmount; } }
public DateTime? SomeDate { get; set; }
}
我刚接触Protobuf-net,是否有什么明显的错误或遗漏?
[ProtoMember(10)] public List<Unit> fUnits;
不能用?我在反序列化包含此字段的对象时遇到了错误。 - David Dunham