【发布时间】:2013-12-30 22:29:05
【问题描述】:
我正在尝试使用 protobuf-net (2.0.0.668) 序列化具有 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)
我错过了什么?
谢谢!
【问题讨论】:
标签: c# serialization protobuf-net