protobuf网创造typemodel接口和抽象基类 [英] Protobuf-net creating typemodel with interface and abstract baseclass

查看:189
本文介绍了protobuf网创造typemodel接口和抽象基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化应用大protobuf网模型。我不能使用属性(对象是未知在编译时),所以我构建了一个TypeModel。 我的对象模型由一类的 TestDataObject 的,这一类具有的 ITestDataExtension 的属性。抽象基类的 TestDataExtensionBase 的实现此接口 和类的 TestDataExtension 的(myDataObjectExtA在code)本基类继承。

I'm trying to serialize a model using the great Protobuf-NET. I cannot use the attributes (objects are unknown at compile-time),so I constructed a TypeModel. My object model consist of a class TestDataObject, this class has a property of ITestDataExtension. The abstract baseclass TestDataExtensionBase implements this interface and the class TestDataExtension (myDataObjectExtA in code) inherits from this baseclass.

我的TypeModel是这样的构造:

My TypeModel is constructed like this:

        System.IO.MemoryStream tmpMemoryStream = new System.IO.MemoryStream();
        RuntimeTypeModel model = TypeModel.Create();
        MetaType basetype = model.Add(typeof(TestDataObject), true);
        MetaType interfaceType = model.Add(typeof(ITestDataExtension), true);
        //MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase));
        MetaType extType = interfaceType.AddSubType(200, myDataObjectExtA.GetType());
        model.Add(typeof(TestDataExtensionBase), true);
        model.Add(myDataObjectA.Ext.GetType(), true);
        model.CompileInPlace();
        model.Serialize(tmpMemoryStream, myDataObjectA);
        byte[] tmpDat = tmpMemoryStream.ToArray();

如果我运行的基类下面的属性是没有序列号,我需要他们进行序列化。
我认为我应该补充一个亚型像这样的TestDataExtensionBase:

If I run the following the properties of the base class are not serialized, and I need them to be serialized.
In my opinion I should have added a subtype for the TestDataExtensionBase like this:

        MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase));
        MetaType extType = extBaseType.AddSubType(200, myDataObjectExtA.GetType());

但是,这导致:意外的子类型:TestDataExtension。 有谁知道什么我做错了吗?任何帮助将是AP preciated。

But this results in a : Unexpected sub-type: TestDataExtension. Does anyone known what I'm doing wrong? Any help would be appreciated.

推荐答案

2的问题:

  • 在接口的​​支持是目前唯一提供的成员的,没有根对象(由于多接口继承问题);解决这个问题的最简单方法是使用包装对象的接口成员
  • ,有必要在模型
  • 定义子类型
  • interface support is only currently provided for members, not root objects (due to the issues of multiple interface inheritance); the easiest way around this is to use a wrapper object with an interface member
  • it is necessary to define the sub-types in the model

我觉得下面做什么你描述......?

I think the following does what you describe...?

using System;
using ProtoBuf.Meta;

interface ITest
{
    int X { get; set; }
}
abstract class TestBase : ITest
{
    public int X { get; set; } // from interface
    public int Y { get; set; }
}
class Test : TestBase
{
    public int Z { get; set; }
    public override string ToString()
    {
        return string.Format("{0}, {1}, {2}", X, Y, Z);
    }
}
class Wrapper
{
    public ITest Value { get; set; }
}
public class Program
{
    static void Main()
    {
        var model = TypeModel.Create();
        model.Add(typeof (ITest), false).Add("X")
                .AddSubType(10, typeof (TestBase));
        model.Add(typeof (TestBase), false).Add("Y")
                .AddSubType(10, typeof (Test));
        model.Add(typeof (Test), false).Add("Z");
        model.Add(typeof (Wrapper), false).Add("Value");

        Wrapper obj = new Wrapper {Value = new Test()
                {X = 123, Y = 456, Z = 789}};

        var clone = (Wrapper)model.DeepClone(obj);
        Console.WriteLine(clone.Value);
    }
}

这篇关于protobuf网创造typemodel接口和抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆