Mat*_*olf 10 c# protocol-buffers system.type protobuf-net
我在尝试序列化时收到以下错误消息List<Tuple<string, Type, object>>:没有为类型定义Serializer:System.Type
我尝试了两种方法,只是序列化上面的集合或序列化一个具有定义为protoMember的相同集合的类.两者都会导致相同的错误消息.
这是不受支持的类型吗?我认为它是受支持的,我忽略了别的东西,但也许我不正确?
感谢任何可能有助于解决此问题的指针......
Mar*_*ell 13
编辑:
Typer580中包含对序列化的支持
protobuf-net旨在序列化您的数据,而不是您的实现; Type是一个实现细节.严格地说,它不会是巨大的努力增加(一些具体实施细节已经基本结束了存储Type信息,通过装配合格的名称),但:它不是一个关键的情况下,而且在许多方面是不是东西,我会鼓励你连载-协议缓冲区的整点是,你可以加载任何平台上的数据,与版本耐受性的重要特征.存储Type信息违反了这两者.
还应该注意的是,大多数其他序列化程序(可能BinaryFormatter已经破坏了平台/版本容忍的每个规则)也将拒绝序列化Type; XmlSerializer,DataContractSerializer,JavaScriptSerializer等全部抛出这种情况下的例外(我只是检查它们).
另外:object是更不支持的,除非你使用DynamicType的功能.
以下是如何通过代理人来完成的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; }
}
Run Code Online (Sandbox Code Playgroud)