2

有这样一种类型:

[ProtoContract(UseProtoMembersOnly=true)]
public class ProtoObjectDTO
{
    [ProtoMember(1, DynamicType=true)]
    public object Value { get; set; }
    [ProtoMember(2)]
    public int Order { get; set; }
}

这些对象的数组具有多个实例(以下代码段中的参数)发送到服务:

await client.PostAsync<ProtoObjectDTO[]>(
    route, parameters, new ProtoBufFormatter())
        .ContinueWith((r) =>
        {
            r.Result.EnsureSuccessStatusCode();

            retVal = true;
        });

parameters包含两个实例,其Value属性设置为不同类型的有效[ProtoContract]对象。

当数据到达服务器端时,它会像这样被反序列化:

var sentParams = ProtoBuf.Serializer.Deserialize<ProtoObjectDTO[]>(stream);

当检查sentParams并与参数进行比较时, 第二个实例中的只是第一个实例中对象类型的默认实例。这很奇怪。你能帮我解决这个问题吗?谢谢你。

4

1 回答 1

0

我添加了以下测试,它成功运行。要进行调查,似乎有必要提供有关所涉及类型的更多信息(或者理想情况下,测试失败):

[ProtoContract]
public class Foo
{
    [ProtoMember(3)]
    public int A { get; set; }
}
[ProtoContract]
public class Bar
{
    [ProtoMember(4)]
    public string B { get; set; }
}

public void Execute()
{
    var model = TypeModel.Create();
    model.AutoCompile = false;

    Execute(model, "Runtime");
    model.CompileInPlace();
    Execute(model, "CompileInPlace");
    Execute(model.Compile(), "Compile");
    model.Compile("SO17040488", "SO17040488.dll");
    PEVerify.AssertValid("SO17040488.dll");

}

private void Execute(TypeModel model, string caption)
{
    var args = new[] {
        new ProtoObjectDTO { Order = 1, Value = new Foo { A = 123 }},
        new ProtoObjectDTO { Order = 2, Value = new Bar { B = "abc" }},
    };
    var clone = (ProtoObjectDTO[])model.DeepClone(args);
    Assert.AreEqual(2, clone.Length, caption + ":length");
    Assert.AreEqual(1, clone[0].Order, caption + ":order");
    Assert.AreEqual(2, clone[1].Order, caption + ":order");
    Assert.IsInstanceOfType(typeof(Foo), clone[0].Value, caption + ":type");
    Assert.IsInstanceOfType(typeof(Bar), clone[1].Value, caption + ":type");
    Assert.AreEqual(123, ((Foo)clone[0].Value).A, caption + ":value");
    Assert.AreEqual("abc", ((Bar)clone[1].Value).B, caption + ":value");
}
于 2013-06-12T06:54:08.057 回答