构建Monodroid应用程序我们一直使用Protobuf-net进行序列化。当fast deply打开时,它在调试模式下工作,但是一旦我们切换到发布模式,它就会失败并出现以下异常:
ex {System.InvalidOperationException: Cannot serialize property without a get accessor
at ProtoBuf.Serializers.PropertyDecorator.SanityCheck (System.Reflection.PropertyInfo property, IProtoSerializer tail, System.Boolean& writeValue, Boolean nonPublic) [0x00000] in <filename unknown>:0
at ProtoBuf.Serializers.PropertyDecorator..ctor (System.Type forType, System.Reflection.PropertyInfo property, IProtoSerializer tail) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.ValueMember.BuildSerializer () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.ValueMember.get_Serializer () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.MetaType.BuildSerializer () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.RuntimeTypeModel.Serialize (Int32 key, System.Object value, ProtoBuf.ProtoWriter dest) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.SerializeCore (ProtoBuf.ProtoWriter writer, System.Object value) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value) [0x00000] in <filename unknown>:0
at ProtoBuf.Serializer.Serialize[UltraliteJCommandParameters] (System.IO.Stream destination, Company.Product.ProtocolBuffers.Android.UltraliteJCommandParameters instance) [0x00000] in <filename unknown>:0
at Company.Product.UltraliteJ.DataCommand.SerializeParameters () [0x00261] in C:\DevSVN\Product\FS\Trunk\FSAndroidSolution\Company.Product.UltraliteJ\DataCommand.cs:109
at Company.Product.UltraliteJ.DataCommand.LoadValue () [0x00001] in C:\DevSVN\Product\FS\Trunk\FSAndroidSolution\Company.Product.UltraliteJ\DataCommand.cs:44
at Company.Product.Ultralite.Controlers.UltraliteDataServer.RunUpgradeScripts (System.String version, Boolean syncSuccessful) [0x0007e] in C:\DevSVN\Product\FS\Trunk\Company.Product.Ultralite.Controlers\UltraliteDataServer.cs:375 } System.InvalidOperationException
我们正在使用.proto文件中生成的代码,它在调试模式下工作。所以我知道我们尝试序列化的那种类型有get访问器,它确实有效。
据我所知,快速部署更改了一个名为“在APK中嵌入程序集”的设置,当设置为false(快速部署模式)时,一切正常。
有没有人遇到过这个问题,有任何解决方法吗?
编辑:
我们的.proto文件:
package Company.ProductName.ProtocolBuffers.Android;
option java_package = "com.company.productname.protocolbuffers.android";
option java_outer_classname = "AndroidParametersProto";
message UltraliteJCommandParameters {
required string command_text = 1;
repeated UltraliteJCommandParameter parameters = 2;
}
message UltraliteJCommandParameter {
required string field_name = 1;
required string data_type = 2;
required bool is_null = 3;
optional double double_value = 4;
optional float float_value = 5;
optional int32 int32_value = 6;
optional int64 int64_value = 7;
optional uint32 uint32_value = 8;
optional uint64 uint64_value = 9;
optional sint32 sint32_value = 10;
optional sint64 sint64_value = 11;
optional fixed32 fixed32_value = 12;
optional fixed64 fixed64_value = 13;
optional sfixed32 sfixed32_value = 14;
optional sfixed64 sfixed64_value = 15;
optional bool bool_value = 16;
optional string string_value = 17;
optional bytes bytes_value = 18;
}
发生故障的代码:
private byte[] SerializeParameters()
{
byte[] paramBytes;
Company.Product.ProtocolBuffers.Android.UltraliteJCommandParameters parameters = new Product.ProtocolBuffers.Android.UltraliteJCommandParameters();
Company.Product.ProtocolBuffers.Android.UltraliteJCommandParameter parameter;
parameters.command_text = CommandText;
foreach (DataParameter param in Parameters)
{
parameter = new Product.ProtocolBuffers.Android.UltraliteJCommandParameter();
parameter.field_name = param.Name;
if (param.Value == null || param.Value == (object)DBNull.Value)
{
parameter.is_null = true;
}
else
{
parameter.is_null = false;
parameter.data_type = param.Value.GetType().ToString().Replace("System.", string.Empty);
switch (param.Value.GetType().ToString())
{
case "System.String":
parameter.string_value = (string)param.Value;
break;
case "System.Int32":
parameter.sint32_value = (int)param.Value;
break;
case "System.Int64":
parameter.sint64_value = (long)param.Value;
break;
case "System.Boolean":
parameter.bool_value = (bool)param.Value;
break;
case "System.DateTime":
parameter.double_value = (((DateTime)param.Value).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime()).TotalMilliseconds;
break;
case "System.Single":
parameter.float_value = (float)param.Value;
break;
case "System.Double":
parameter.double_value = (double)param.Value;
break;
}
}
parameters.parameters.Add(parameter);
}
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<Company.Product.ProtocolBuffers.Android.UltraliteJCommandParameters>(stream, parameters);
paramBytes = stream.ToArray();
}
return paramBytes;
}
答案 0 :(得分:2)
Mono for Android中使用的(托管)链接器将删除未使用的代码以创建更小的应用程序。此删除过程(和选择)基于静态分析,并且不会找到仅由反射使用的内容。
尝试设置选项以禁用链接器,即将其设置为“不要链接”,然后重试。如果它有效,那么这可能就是问题。
如果是这种情况,您可以告诉链接器忽略该属性所在的程序集(命令行选项应该命名为--linkskip
),或者您可以在您的上添加[Preserve]
属性属性(或类型)告诉链接器不要删除代码(即使它看起来未使用)。