protobuf-net 在Unity里使用 int64(long), 在IOS设备上解析错乱问题。。 Mono在IOS反射机制bug
C#里解析 protobuf 用了 protobuf-net, 一直使用正常的。。 但最近在苹果手机上, 出现莫名其妙问题。
最后发现是 传的Int64字段 解析出来要么很大很大一个,反正不对。。
跟踪 发现
PropertyDecorator.cs里
object newVal = Tail.Read(oldVal, source);
if (readOptionsWriteValue && newVal != null) // if the tail returns a null, intepret that as *no assign*
{
if (shadowSetter == null)
{
property.SetValue(value, newVal, null);
}
else
{
shadowSetter.Invoke(value, new object[] { newVal });
}
}
newVal 读到的是对的。。。。
走到 property.SetValue (反射机制 赋值), 结果里面得到的值就是一个很大很乱的数值。。。。
看来不能用 protobuf-net 或者不要用 有64位字段。
有没有好的Unity里用 protobuf 的解决方案?
目前我们用的是Unity3D 5.3
应该是Mono实现反射机制有问题。 直接写了一个测试代码:
public class Test_Reflect
{
long id = 0;
public long ID
{
get { return id; }
set {
ClientLog.Log("@@@@ set_ID:" + value);
id = value; }
}
public void set_id1(long _i)
{
ClientLog.Log("@@@@ set_id1:" + _i);
id = _i;
}
public void set_id2(System.Int64 _i)
{
ClientLog.Log("@@@@ set_id2:" + _i);
id = _i;
}
}
Test_Reflect tr = new Test_Reflect();
long tt = 1000000000L;
foreach (PropertyInfo property in tr.GetType().GetProperties())
{
//输出属性的类型和变量名
ClientLog.Log("@@@@ test reflect "+property.Name + " : " + property.PropertyType.ToString());
property.SetValue(tr, tt, null);
}
tr.GetType().GetMethod("set_id1").Invoke(tr, new object[] { tt+1 });
tr.GetType().GetMethod("set_id2").Invoke(tr, new object[] { tt + 2 });
在IOS设备上运行, 只要经过反射机制得到的值都是不对的。。。。