【发布时间】:2017-07-23 09:36:42
【问题描述】:
我正在尝试使用 protobuf 序列化下面的类,但由于“对象引用”错误而失败。更多细节如下。通过查看错误详细信息,知道可能出了什么问题吗? 注意:我的用户对象太大,它有很多子对象和属性。所以不要在此处添加 User 类的详细信息。
[ProtoContract]
public class MyPrincipal
{
public MyPrincipal(string Id, int myId)
{
this.Id = Id;
this.MyId = myId;
this.Principals = new Dictionary<MyPrincipalType, User>();
}
[ProtoMember(1)]
public string Id { get; set; }
[ProtoMember(2)]
public int MyId { get; set; }
[ProtoMember(3)]
public Dictionary<MyPrincipalType, User> Principals { get; set; }
public MyPrincipal AddPrincipal(MyPrincipalType principalType, User principalValue)
{
this.Principals.Add(principalType, principalValue);
return this;
}
}
public enum MyPrincipalType
{
Test = 0
}
以下是错误详情:
异常: {“对象引用未设置为对象的实例。”}
Inner StrackTrace : 在 ProtoBuf.Serializers.TagDecorator.get_ExpectedType() 在 ProtoBuf.Serializers.DefaultValueDecorator..ctor(TypeModel 模型,对象 defaultValue,IProtoSerializer 尾部) 在 ProtoBuf.Serializers.MapDecorator`3..ctor(TypeModel 模型,类型concreteType,IProtoSerializer keyTail,IProtoSerializer valueTail,Int32 fieldNumber,WireType wireType,WireType keyWireType,WireType valueWireType,Boolean overwriteList)
外部 StackTrace: 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object [] 参数,CultureInfo 文化)在 System.Reflection.ConstructorInfo.Invoke(对象 [] 参数)在 ProtoBuf.Meta.ValueMember.BuildSerializer() 在 ProtoBuf.Meta.ValueMember.get_Serializer() 在 ProtoBuf.Meta.MetaType.BuildSerializer( ) 在 ProtoBuf.Meta.MetaType.get_Serializer() 在 ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) 在 ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) 在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) 在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value) 在 ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
此问题仅适用于最新的 nuget 版本 2.3.0。当我使用 2.0.0.668 版本时,它工作正常。
【问题讨论】:
-
感谢您的快速回复。试过但同样的错误。还有其他建议吗?
-
您的代码无法编译,因为您没有包含
User的定义。如果我只是添加一个没有属性的类型,我会得到一个ProtoBuf.ProtoException: No parameterless constructor found for MyPrincipal,正如预期的那样:fiddle。如果我添加一个默认构造函数,也不例外:fiddle #2。请提供实际重现问题的minimal reproducible example。 -
当然。将粘贴示例以重现问题...并且 Principals 属性具有属性,但在发布问题时错过了。
-
另外,当我将我的属性从 public Dictionary
Principals 更改为 public Dictionary Principals 时,这适用于最新的 nuget 版本。有什么建议吗?
标签: protobuf-net proto protobuf-csharp-port