简体   繁体   English

ProtoBuf-Net 错误:没有为内部 class 类型定义序列化程序

[英]ProtoBuf-Net error: No serializer defined for type of inner class

When using protobuf-net, I have a class Data decorated with [ProtoContract] attribute, which defines an inner class.使用 protobuf-net 时,我有一个用[ProtoContract]属性装饰的 class Data ,它定义了一个内部 class。 Members of that type should be serialized as well.该类型的成员也应该被序列化。

[ProtoContract] 
public class Data {
    [ProtoMember(1)]
    public string Member { get; set; }
    
    [ProtoMember(2)]
    public NestedData Nested { get; set; }

    public class NestedData
    {
        [ProtoMember(1)]
        public string InnerMember { get; set; }
    } 
}

When I instantiate this class and try to serialize it using ProtoBuf.Serializer.Serialize(System:IO.Stream, Data) , this raises an exception "System.InvalidOperationException: 'No serializer defined for type: MinimalExample.Data+NestedData'". When I instantiate this class and try to serialize it using ProtoBuf.Serializer.Serialize(System:IO.Stream, Data) , this raises an exception "System.InvalidOperationException: 'No serializer defined for type: MinimalExample.Data+NestedData'". I guess that I just have a blind spot - where is my error?我想我只是有一个盲点——我的错误在哪里? I would be rather surprised if protobuf-net had a problem with handling inner classes in general.如果 protobuf-net 通常在处理内部类时遇到问题,我会感到相当惊讶。

I am using protobuf-net 3.0.101 on .net 5.我在 .net 5 上使用 protobuf-net 3.0.101。

You just need to annotate NestedData with [ProtoContract] :您只需要使用[ProtoContract]注释NestedData


[ProtoContract]
public class Data
{
    [ProtoMember(1)]
    public string Member { get; set; }

    [ProtoMember(2)]
    public NestedData Nested { get; set; }

    [ProtoContract]
    public class NestedData
    {
        [ProtoMember(1)]
        public string InnerMember { get; set; }
    }
}

Test:测试:

void Main()
{
    var data = new Data
    {
        Member = "member",
        Nested = new Data.NestedData
        {
            InnerMember = "inner"
        }
    };


    using var mem = new MemoryStream();
    ProtoBuf.Serializer.Serialize(mem, data);
    mem.Position = 0;

    var data2 = ProtoBuf.Serializer.Deserialize<Data>(mem);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProtoBuf-Net:没有为类型定义序列化程序:System.Object - ProtoBuf-Net: No serializer defined for type: System.Object Protobuf-Net错误消息:没有为类型定义Serializer:System.Type - Protobuf-Net error message: No Serializer defined for type: System.Type 对象(通用)的 Protobuf-net 序列化抛出错误没有为类型定义序列化程序:System.Object - Protobuf-net serialization of object (generic) throws error No serializer defined for type: System.Object 没有为Protobuf-net中的类型:System.Management.Automation.PSObject定义的序列化程序 - No serializer defined for type: System.Management.Automation.PSObject in Protobuf-net protobuf-net 异常:没有为类型定义序列化程序:System.Xml.XmlDocument - protobuf-net exception: No serializer defined for type: System.Xml.XmlDocument “类型不正确,无法推断出任何合同:Leap.Vector”错误。 使用Protobuf-net序列化器 - “Type is not expected, and no contract can be inferred: Leap.Vector” error. using Protobuf-net serializer 在protobuf-net中序列化Type类? - Serializing the Type class in protobuf-net? protobuf-net:枚举类型转换错误 - protobuf-net : enum type conversion error 使用protobuf-net序列化具有接口类型成员的类 - Serialize a class having an interface type member using protobuf-net protobuf-net 从基础 class 中跳过一个成员(接口类型) - protobuf-net skips a member (of Interface type) from base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM