问题
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