未为以下类型定义串行器

未为以下类型定义串行器

本文介绍了Protobuf-Net错误消息:未为以下类型定义串行器:System.Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试序列化 List< Tuple< string,Type,object>> 时,我收到以下错误消息:没有为类型定义任何序列化程序:System.Type

I am getting the following error message when trying to serialize List<Tuple<string, Type, object>>: No Serializer defined for type: System.Type

我都尝试过,只是序列化以上集合或序列化具有与protoMember定义相同集合的类.两者都导致相同的错误消息.

I tried both, just serializing the above collection or serializing a class that has the same collection defined as protoMember. Both result in the same error message.

这是不受支持的类型吗?我认为它受支持,但我忽略了其他内容,但也许我不正确吗?

Is this a non-supported type? I assume it is supported and I overlooked something else but maybe I am incorrect?

感谢所有可能有助于解决此问题的指针...

Thanks for any pointers that may help resolve this...

推荐答案

r580中包含对 Type 序列化的支持

Support for Type serialization is included in r580

protobuf-net用于序列化您的数据,而不是您的实现; Type 是实现的详细信息.严格来说,添加并不是很困难(一些特定于实现的细节实际上已经可以通过Assembly-qualified-name存储 Type 信息了),但是:它不是一个关键的场景,在许多方面,我都不鼓励您进行序列化.我建议您进行序列化-协议缓冲区的全部意义在于,您可以在任何平台上加载数据,而版本公差是关键特性.存储 Type 信息违反了这两项.

protobuf-net is intended to serialize your data, not your implementation; Type is an implementation detail. Strictly speaking, it wouldn't be hugely hard to add (some of the implementation-specific details already essentially end up storing Type info, via the assembly-qualified-name), but: it isn't a key scenario, and in many ways is not something I would encourage you to serialize - the whole point of protocol buffers is that you can load the data on any platform, with version tolerance a key feature. Storing Type information violates both of these.

还应注意,大多数 other 序列化程序(也许 BinaryFormatter 除外,它已经破坏了平台/版本容忍的所有规则)将 也拒绝序列化 Type XmlSerializer DataContractSerializer JavaScriptSerializer 全部 为此情况抛出了异常(我刚刚检查了它们.)

It should also be noted that most other serializers (except perhaps BinaryFormatter, which already breaks every rule of platform/version-tolerance) will also refuse to serialize a Type; XmlSerializer, DataContractSerializer, JavaScriptSerializer etc all throw an exception for this scenario (I just checked them).

另外:除非您使用 DynamicType 功能,否则 object 的支持程度甚至会更低 .

Additionally: object is even less supportable, unless you use the DynamicType feature.

这是通过 Type 上的代理完成的方法:

Here's how it could be done via a surrogate on Type:

using ProtoBuf;
using ProtoBuf.Meta;
using System;
using System.Runtime.Serialization;

static class Program
{
    public static void Main(string[] args)
    {
        // register a surrogate for Type
        RuntimeTypeModel.Default.Add(typeof(Type), false)
                                .SetSurrogate(typeof(TypeSurrogate));
        // test it
        var clone = Serializer.DeepClone(new Foo { Type = typeof(string) });
    }
}

[ProtoContract]
class TypeSurrogate
{
    [ProtoMember(1)]
    public string AssemblyQualifiedName { get; set; }
    // protobuf-net wants an implicit or explicit operator between the types
    public static implicit operator Type(TypeSurrogate value)
    {
        return value==null ? null : Type.GetType(value.AssemblyQualifiedName);
    }
    public static implicit operator TypeSurrogate(Type value)
    {
        return value == null ? null : new TypeSurrogate {
            AssemblyQualifiedName  = value.AssemblyQualifiedName };
    }
}

[DataContract]
public class Foo
{
    [DataMember(Order=1)]
    public Type Type { get; set; }
}

这篇关于Protobuf-Net错误消息:未为以下类型定义串行器:System.Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:32