在protobuf网如何传递对象类型的数组与不同类型的内部对象,事先知道该组潜在的类型 [英] In Protobuf-net how can I pass an array of type object with objects of different types inside, knowing the set of potential types in advance

查看:1425
本文介绍了在protobuf网如何传递对象类型的数组与不同类型的内部对象,事先知道该组潜在的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想迁移使用XmlSerializer的到protobuf网,由于它提供了更高的性能现有code,但我有这种特殊情况下的问题。

I am trying to migrate existing code that uses XmlSerializer to protobuf-net due to the increased performance it offers, however I am having problems with this specific case.

我有一个对象[],其包括将要发送到远程主机(排序的自定义迷你RPC设施的)参数。我知道类型的集合,从这些参数就可以了,但我不能事先在他们将要发送的顺序告诉。
我有三个约束。首先,我在Compact Framework中正在运行,所以我需要的东西,在那里工作。其次,正如我所说的表现是一个大问题(在串行化侧),所以我宁愿避免使用大量的反射,如果有可能的。而最重要的是,我关心此参数的发送顺序。
使用XmlSerializer的很容易只需添加XmlInclude,但场没有什么作为等价据我所知,在protobuf网。那么,有没有办法做到这一点?下面是一个简单的例子。

I have an object[] that includes parameters that are going to be sent to a remote host (sort of a custom mini rpc facility). I know the set of types from which these parameters can be, but I cannot tell in advance in which order they are going to be sent. I have three constraints. The first is that I am running in Compact Framework, so I need something that works there. Second, as I mentioned performance is a big concern (on the serializing side) so I would rather avoid using a lot of reflection there if possible. And the most important is that I care about the order in which this parameters were sent. Using XmlSerializer it was easy just adding XmlInclude, but for fields there is nothing equivalent as far as I know in Protobuf-net. So, is there a way to do this? Here is a simplified example.

    [Serializable]
    [XmlInclude(typeof(MyType1)),
     XmlInclude(typeof(MyType2)),
     XmlInclude(typeof(MyType3))
    public class Message()
    {
         public object[] parameters;

         public Message(object[] parms)
         {
             parameters = parms; 
         }
    }

    Message m = new Message(new object[] {MyType1(), 33, "test", 
                new MyType3(), new MyType3()});
    MemoryStream ms = new MemoryStream();
    XmlSerializer xml = new XmlSerializer(typeof(Message));
    xml.Serialize(ms,xml);

这将只是XmlSerializer的工作,但如果我尝试将其转换为protobuf网我会得到一个没有默认编码对象的消息。

That will just work with XmlSerializer, but if I try to convert it to protobuf-net I will get a "No default encoding for Object" message.

我想出最好是使用泛型和[ProtoInclude]如看到此<一个href=\"http://stackoverflow.com/questions/1296791/protobuf-attributes-with-a-hierarchy-of-generic-classes/1296845#1296845\">example.因为我可以在阵列内有不同的对象类型,这并不十分成功的。我添加了一个通用的列出每个可能的类型,与[ProtoIgnore]属性与类型的对象[]添加并得到他们。我加入的时候他们使用反射(要知道在数组把每一个项目),这是不可取的,我仍然无法preserve的排序,因为我只是一个提取每个列表中的一个上的所有项目,并把他们进入的财产得到一个新的对象[]数组。

The best I came up with is to use generics and [ProtoInclude] as seen in this example. Since I can have different object types within the array this doesn't quite make it. I added a generic List for each potential type and a property with [ProtoIgnore] with type object[] to add them and get them. I have to use reflection when adding them (to know in which array to put each item) which is not desirable and I still can't preserve the ordering as I just extract all the items on each list one by one and put them into a new object[] array on the property get.

我不知道是否有做到这一点的方法吗?

I wonder if there is a way to accomplish this?

我想什么马克以下建议,但我无法得到它的工作。我想我可能有一些误解。

I tried what Marc suggested below, but I couldn't get it to work. I think I may have misunderstood something.

使用code你写。我想我应该用MessageParam创建生成MessageParam对象添加到列表中。所以基本上我添加了一个构造函数的消息是这样的:

Using the code you wrote. I thought I should use MessageParam Create to generate MessageParam objects to add to the list. So basically I added a constructor to Message like this:

public Message(object[] parms)
{
    foreach (object o in parms)
    {
        parameters.Add(MessageParam.Create(o));
    }
}

但是,如果我这样做,我会得到序列化过程中发现意外的类型;类型必须包含ProtoIncludeAttribute;如发现通过MessageParam MessageParam`1,因为我认为串行期待非通用版本。我误解你的建议?如果是这样,什么是应该做的正确的事?

But, if i do that I will get "Unexpected type found during serialization; types must be included with ProtoIncludeAttribute; found MessageParam`1 passed as MessageParam" because I assume the serializer is expecting the non-generic version. Did I misunderstand your suggestion? If so, what is the right thing to do?

推荐答案

对象将是有问题的。我会尝试更多的东西,如:

object is going to be problematic. I would try something more like:

[ProtoContract]
class Message
{
    private readonly List<MessageParam> parameters = new List<MessageParam>();
    [ProtoMember(1)]
    public List<MessageParam> Parameters { get { return parameters; } }
}
[ProtoContract]
[ProtoInclude(3, typeof(MessageParam<int>))]
[ProtoInclude(4, typeof(MessageParam<float>))]
[ProtoInclude(5, typeof(MessageParam<DateTime>))]
//...known types...
abstract class MessageParam {
    public abstract object UntypedValue { get; set; }
    public static MessageParam<T> Create<T>(T value) {
        return new MessageParam<T> { Value = value };
    }
    public static MessageParam CreateDynamic(object value)
    {
        Type type = value.GetType();
        switch (Type.GetTypeCode(value.GetType()))
        {
            // special cases
            case TypeCode.Int32: return Create((int)value);
            case TypeCode.Single: return Create((float)value);
            case TypeCode.DateTime: return Create((DateTime)value);
            // fallback in case we forget to add one, or it isn't a TypeCode
            default:
                MessageParam param = (MessageParam)Activator.CreateInstance(
                    typeof(MessageParam<>).MakeGenericType(type));
                param.UntypedValue = value;
                return param;
        }
    }
}
[ProtoContract]
sealed class MessageParam<T> : MessageParam
{
    [ProtoMember(1)]
    public T Value { get; set; }
    public override object UntypedValue
    {
        get { return Value; }
        set { Value = (T)value; }
    }
}

请注意,未发行的V2code提供了更多的定义在运行时,而不是通过属性的关系(这是相当限制在这里)的能力。

Note that the unreleased "v2" code offers much more ability to define the relationships at runtime rather than through attributes (which is quite limiting here).

这篇关于在protobuf网如何传递对象类型的数组与不同类型的内部对象,事先知道该组潜在的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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