C# 序列化动态类型参数Protobuf-net
没有答案的可能重复:和 我试图使用protobuf net(2.0.0.668)用C# 序列化动态类型参数Protobuf-net,c#,serialization,protobuf-net,C#,Serialization,Protobuf Net,没有答案的可能重复:和 我试图使用protobuf net(2.0.0.668)用params对象数组序列化一个类 我的参数对象[]中可以有不同的类型 使用DataContractSerializer时,只需使用[KnownType]即可正常工作 我知道protobuf net的情况并非如此,我必须使用[ProtoInclude]以及DynamicType=true,如下所示: [ProtoContract, ProtoInclude(20, typeof(Int32))] //Int32 as
params
对象数组序列化一个类
我的参数对象[]中可以有不同的类型
使用DataContractSerializer时,只需使用[KnownType]
即可正常工作
我知道protobuf net的情况并非如此,我必须使用[ProtoInclude]
以及DynamicType=true
,如下所示:
[ProtoContract, ProtoInclude(20, typeof(Int32))] //Int32 as an example
public class MyParams
{
public MyParams(){}
public MyParams(
string name,
params object[] parms)
{
this.Name = name;
this.Parms = parms;
}
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2, DynamicType = true)]
public object[] Parms { get; set; }
}
奇怪的是,每当我在对象数组中传递一些字符串时,它都会工作,但如果我给它其他任何东西,它就会失败(本例中为Int32)
这是它抛出的异常:
Exception:Thrown: "Dynamic type is not a contract-type: Int32 (System.InvalidOperationException)
我错过了什么
谢谢 动态类型的当前实现不支持原语。它只支持合同类型(其他类以某种方式被定义为
ProtoContract
)
发件人:
DynamicType-与类型一起存储其他类型信息(默认情况下,它包括AssemblyQualifiedName,尽管这可以由用户控制)。这使得序列化弱模型成为可能,即对象用于属性成员的位置,但目前这仅限于约定类型(而非原语),并且不适用于具有继承的类型(这些限制可能在以后删除)。与AsReference一样,它使用了非常不同的布局格式
可能重复的