相关疑难解决方法(0)

protobuf-net 的 [ProtoInclude(1, "MyClass")] 不起作用

我正在将 protobuf-net v2 beta r431 用于 C# .net 4.0 应用程序。在我的应用程序中Dictionary<int, IMyClass>,我需要序列化一个。一个类MyClass实现IMyClass接口。根据 protobuf 的文档,我编写的代码如下:

[ProtoContract]
[ProtoInclude(1, typeof(MyClass))]
public interface IMyClass
{
    int GetId();
    string GetName();
}

[ProtoContract]
[Serializable]
public class MyClass : IMyClass
{
    [ProtoMember(1)]
    private int m_id = 0;

    [ProtoMember(2)]
    private string m_name = string.Empty;

    public MyClass(int id, string name)
    {
        m_id = id;
        m_name = name;
    }

    public MyClass()
    {
    }

    #region IMyClass Members

    public int GetId()
    {
        return m_id;
    }

    public string …
Run Code Online (Sandbox Code Playgroud)

c# protobuf-net

5
推荐指数
2
解决办法
1658
查看次数

ProtoBuf-Net ProtoInclude 泛型类型子类

我在使用从泛型类继承的对象的子类的 ProtoBuf-Net 时遇到了一些问题。

我的继承树看起来像这样:

Node
    SomeNodeType
    SomeOtherType
    ResourceNode<T>
        ShipResource : ResourceNode<Ship>
        SomeResource : ResourceNode<SomeType>
Run Code Online (Sandbox Code Playgroud)

我一直在所有普通类型的基本 Node 类型上使用 ProtoInclude。

使用 protobuf-net 实现这种层次结构的最佳方法是什么?我试过只包含所有内容,但我得到的错误似乎源于 protobuf 试图将对象反序列化为它的父对象之一。

protobuf-net

5
推荐指数
1
解决办法
4530
查看次数

标签 统计

protobuf-net ×2

c# ×1