C# ProtoBuf、继承和泛型

C# ProtoBuf、继承和泛型,c#,protocol-buffers,protobuf-net,C#,Protocol Buffers,Protobuf Net,给定以下代码: [Serializable, ProtoContract] [ProtoInclude(100, typeof(ValueA))] [ProtoInclude(101, typeof(ValueB))] public class Value { } [Serializable, ProtoContract] public class ValueA : Value { } [Serializable, ProtoContract] public class ValueB : V

给定以下代码:

[Serializable, ProtoContract]
[ProtoInclude(100, typeof(ValueA))]
[ProtoInclude(101, typeof(ValueB))]
public class Value
{
}

[Serializable, ProtoContract]
public class ValueA : Value
{
}

[Serializable, ProtoContract]
public class ValueB : Value
{
}

[Serializable, ProtoContract]
[ProtoInclude(1000, typeof(ContainerA))]
[ProtoInclude(1001, typeof(ContainerB))]
public class Container<T> where T : Value
{
    [ProtoMember(1)]
    public T Item;
}

[Serializable, ProtoContract]
public class ContainerA : Container<ValueA>
{
}

[Serializable, ProtoContract]
public class ContainerB : Container<ValueB>
{
}
这里发生了什么?我们能做些什么


TIA

我仍在等待更多信息,但最终从开放泛型类型继承有点棘手。也许如果我了解确切的模型,我可以添加更多内容,但以下内容有效:

using System.Diagnostics;
using ProtoBuf;
[ProtoContract]
[ProtoInclude(1, typeof(ValueA))]
[ProtoInclude(2, typeof(ValueB))]
public class Value
{
}

[ProtoContract]
public class ValueA : Value
{
}

[ProtoContract]
public class ValueB : Value
{
}
[ProtoContract]
[ProtoInclude(1, typeof(Container<ValueA>))]
[ProtoInclude(2, typeof(Container<ValueB>))]
public abstract class Container
{

    public abstract Value BaseValue { get; set; }
}
[ProtoContract]
public class Container<T> : Container where T : Value
{
    [ProtoMember(1)]
    public T Value { get; set;}

    public override Value BaseValue
    {
        get { return Value; }
        set { Value = (T)value; }
    }
}

static class Program
{
    static void Main()
    {
        var model = new Container<ValueA>();
        model.Value = new ValueA();
        var clone = Serializer.DeepClone(model);
        Debug.Assert(clone.Value is ValueA);
    }
}

我仍在等待更多信息,但最终从开放泛型类型继承有点棘手。也许如果我了解确切的模型,我可以添加更多内容,但以下内容有效:

using System.Diagnostics;
using ProtoBuf;
[ProtoContract]
[ProtoInclude(1, typeof(ValueA))]
[ProtoInclude(2, typeof(ValueB))]
public class Value
{
}

[ProtoContract]
public class ValueA : Value
{
}

[ProtoContract]
public class ValueB : Value
{
}
[ProtoContract]
[ProtoInclude(1, typeof(Container<ValueA>))]
[ProtoInclude(2, typeof(Container<ValueB>))]
public abstract class Container
{

    public abstract Value BaseValue { get; set; }
}
[ProtoContract]
public class Container<T> : Container where T : Value
{
    [ProtoMember(1)]
    public T Value { get; set;}

    public override Value BaseValue
    {
        get { return Value; }
        set { Value = (T)value; }
    }
}

static class Program
{
    static void Main()
    {
        var model = new Container<ValueA>();
        model.Value = new ValueA();
        var clone = Serializer.DeepClone(model);
        Debug.Assert(clone.Value is ValueA);
    }
}

顺便说一句,protobuf网络的哪个版本?只是想知道为什么这个消息有点古怪……封闭泛型类型并不是直接从它们的开放泛型定义继承来的——它有点复杂。你能澄清一下你想模拟的是什么吗?还是代码说明了一切?嗨,马克!代码准确地说明了我遇到的问题,但是为了理解“现实”中发生了什么,我应该写“公共列表项”,而不仅仅是“公共T项”。(尽管在真实代码中,我们还有一个继承级别)。因此,我们正在对不同类型的“时间序列”进行建模,在这里我们可以有一些不同类型的“时间序列值”-如果这更清楚的话?…也许我应该提到,所有类都包含代码和属性-它们不仅仅是如上图所示的空的,顺便说一句,是哪个版本的protobuf net?只是想知道为什么这个消息有点古怪……封闭泛型类型并不是直接从它们的开放泛型定义继承来的——它有点复杂。你能澄清一下你想模拟的是什么吗?还是代码说明了一切?嗨,马克!代码准确地说明了我遇到的问题,但是为了理解“现实”中发生了什么,我应该写“公共列表项”,而不仅仅是“公共T项”。(尽管在真实代码中,我们还有一个继承级别)。所以我们在建模不同类型的“时间序列”,在这里我们可以有一些不同类型的“时间序列值”-如果这让事情更清楚的话?…也许我应该提到所有类都包含代码和属性-它们不仅仅是如上图所示的空,自回复日期以来有什么变化吗?层次结构末尾的封闭泛型通常很有用,但它似乎仍然没有用ProtoBuf.net序列化-虽然没有像原始问题中那样出现错误,但我仍然在Container.Marc的公共T值{get;set;}中得到Null,自回复日期以来有什么变化吗?层次结构末尾的封闭泛型通常很有用,但它似乎仍然没有用ProtoBuf.net进行序列化-虽然没有像原始问题中那样出现错误,但我仍然从容器中获取公共T值{get;set;}中的Null。
using System.Diagnostics;
using ProtoBuf;

[ProtoContract]
public class ValueA
{
}

[ProtoContract]
public class ValueB
{
}

[ProtoContract]
[ProtoInclude(1, typeof(Container<ValueA>))]
[ProtoInclude(2, typeof(Container<ValueB>))]
public abstract class Container
{

    public abstract object BaseValue { get; set; }
}
[ProtoContract]
public class Container<T> : Container
{
    [ProtoMember(1)]
    public T Value { get; set;}

    public override object BaseValue
    {
        get { return Value; }
        set { Value = (T)value; }
    }
}

static class Program
{
    static void Main()
    {
        var model = new Container<ValueA>();
        model.Value = new ValueA();
        var clone = Serializer.DeepClone(model);
        Debug.Assert(clone.Value is ValueA);
    }
}