Protobuf-net: Using Hidden Members does not work

我与影子孤独终老i 提交于 2019-12-08 06:45:47

问题


Using types with hidden fields leads to an "Unable to determine member" Exception (V2 via RuntimeTypeModel without any attributes).

Removing the Length check in this line in MetaType.cs solves the problem for this special use case.

 if(members != null && members.Length == 1) mi = members[0];

But I fear there is a reason for the length check, so simply removing it might not be an adequate solution. Using members[0] works in this case, because the hidden base class members go at the end of the array.

Simplified Example of my use case:

public class SharedType
{
    public int Number { get; set; }
}

public class ClientType : SharedType
{
    public new long Number { get; set; }
}

var modelServer = RuntimeTypeModel.Create();
modelServer.Add(typeof(SharedType), false).Add("Number");

var ms = new MemoryStream();
modelServer.Serialize(ms, new SharedType() { Number = 10 });

var modelClient = RuntimeTypeModel.Create();
modelClient.Add(typeof(ClientType), false).Add("Number");

ClientType obj = (ClientType)modelClient.Deserialize(new MemoryStream(ms.ToArray()), null, typeof(ClientType));

In reality i would replace int by SharedNestedType and long by ClientNestedType.

来源:https://stackoverflow.com/questions/14735209/protobuf-net-using-hidden-members-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!