在 Protobuf-net 中,我如何传递一个类型对象数组,其中包含不同类型的对象,提前知道潜在类型的集合

发布于 2024-08-29 23:37:45 字数 1873 浏览 6 评论 0原文

我正在尝试将使用 XmlSerializer 的现有代码迁移到 protobuf-net,因为它提供了更高的性能,但是我在这种特定情况下遇到了问题。

我有一个对象[],其中包含将发送到远程主机(类似于自定义迷你 rpc 设施)的参数。我知道这些参数可以来自哪些类型,但我无法提前知道它们将按什么顺序发送。 我有三个限制。首先是我在 Compact Framework 中运行,所以我需要一些可以在那里工作的东西。其次,正如我提到的,性能是一个大问题(在序列化方面),因此如果可能的话,我宁愿避免在那里使用大量反射。最重要的是我关心这些参数的发送顺序。 使用 XmlSerializer 只需添加 XmlInclude 就很容易,但对于字段,据我所知,Protobuf-net 中没有任何等效项。那么,有办法做到这一点吗?这是一个简化的示例。

    [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-net,我将收到“对象没有默认编码”消息。

我想出的最好的方法是使用泛型和 [ProtoInclude],如 示例。由于我可以在数组中拥有不同的对象类型,这并不完全成功。我为每个潜在类型添加了一个通用列表,并添加了一个带有 [ProtoIgnore] 和 type object[] 的属性来添加它们并获取它们。添加它们时我必须使用反射(以知道将每个项目放在哪个数组中),这是不可取的,而且我仍然无法保留顺序,因为我只是一一提取每个列表上的所有项目并将它们放入属性 get 上的新 object[] 数组。

我想知道是否有办法实现这一点?


我尝试了马克下面的建议,但我无法让它发挥作用。我想我可能误会了什么。

使用您编写的代码。我想我应该使用 MessageParam Create 来生成 MessageParam 对象以添加到列表中。所以基本上我向 Message 添加了一个构造函数,如下所示:

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

但是,如果我这样做,我将得到“序列化期间发现意外类型;类型必须包含在 ProtoIncludeAttribute 中;发现 MessageParam`1 作为 MessageParam 传递”,因为我假设序列化程序期望非通用版本。我误解了你的建议吗?如果是这样,正确的做法是什么?

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.

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);

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.

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.

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));
    }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2024-09-05 23:37:45

object 将会出现问题。我会尝试更像这样的事情:

[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; }
    }
}

请注意,未发布的“v2”代码提供了更多在运行时定义关系的能力,而不是通过属性(这在这里非常有限)。

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; }
    }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文