我很难让应用程序使用完全链接。这是我的设置(我的程序集名称已更改(:
- 适用于Android的Mono 4.4.54
- 安卓应用程序(MyApp(
- 平台无关库(MyLib(
我正在尝试使用ServiceStack.text从JSON文本反序列化一个类型(Person(。它在仅链接SDK程序集时工作良好。
我不是在MyLib中放置链接器包含属性,而是在MyApp使用LinkDescriptionXML文件。它看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="MyLib">
<type fullname="MyLib.Person" preserve="fields">
<method name=".ctor" />
</type>
</assembly>
</linker>
这是我的logcat输出:
E/MyApp(14494): InitialiseUser(): System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.Text.Json.JsonReader`1 ---> System.ArgumentNullException: Argument cannot be null.
E/MyApp(14494): Parameter name: method
E/MyApp(14494): at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0
E/MyApp(14494): at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.PlatformExtensions.MakeDelegate (System.Reflection.MethodInfo mi, System.Type delegateType, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Json.JsonReader.GetParseFn (System.Type type) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Json.JsonTypeSerializer.GetParseFn (System.Type type) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Common.TypeAccessor.Create (ITypeSerializer serializer, ServiceStack.Text.TypeConfig typeConfig, System.Reflection.PropertyInfo propertyInfo) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Common.DeserializeTypeRef.GetTypeAccessorMap (ServiceStack.Text.TypeConfig typeConfig, ITypeSerializer serializer) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Common.DeserializeType`1[ServiceStack.Text.Json.JsonTypeSerializer].GetParseMethod (ServiceStack.Text.TypeConfig typeConfig) [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Common.JsReader`1[ServiceStack.Text.Json.JsonTypeSerializer].GetCoreParseFn[Person] () [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Common.JsReader`1[ServiceStack.Text.Json.JsonTypeSerializer].GetParseFn[Person] () [0x00000] in <filename unknown>:0
E/MyApp(14494): at ServiceStack.Text.Json.JsonReader`1[MyLib.Person]..cctor () [0x00000] in <filename unknown>:0
E/MyApp(14494): --- End of inner exception stack trace ---
E/MyApp(14494): at ServiceStack.Text.JsonSerializer.DeserializeFromString[Person] (System.String value) [0x00000] in <filename unknown>:0
E/MyApp(14494): at MyLib.Person.FromJson (System.String json, Boolean throwOnNullOrEmpty) [0x00000] in <filename unknown>:0
E/MyApp(14494): at MyApp.MainActivity.InitialiseUser () [0x00000] in <filename unknown>:0
在此之后,我的Person实例为null。
从输出中似乎找不到Person的默认构造函数(它没有显式定义的构造函数(。
非常感谢您的帮助。
实际上是ServiceStack.Text本身被链接器搞砸了。JsonReader<T>.GetParseFn()
是通过反射访问的,这显然导致了它的遗漏。
我去掉了LinkerDescriptionXML,并添加了一个类,如下所示:
using ServiceStack.Text.Common;
using ServiceStack.Text.Json;
namespace MyApp
{
public class LinkerInclude
{
public ParseStringDelegate IncludeJsonReader<T>()
{
return JsonReader<T>.GetParseFn();
}
}
}
最后,我发现了更多的问题,这些问题我无法通过之前的答案中的代码方法解决。这是因为链接器忽略的某些类是ServiceStack.Text程序集的内部类。
最终,我恢复使用链接描述XML。它看起来是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="ServiceStack.Text">
<type fullname="ServiceStack.Text.Json.JsonReader`1" >
<method name="GetParseFn" />
</type>
<type fullname="ServiceStack.Text.Common.DeserializeListWithElements`2" >
<method name="ParseGenericList" />
</type>
<type fullname="ServiceStack.Text.Common.WriteListsOfElements`2" >
<method name="WriteList" />
<method name="WriteArray" />
</type>
</assembly>
</linker>
我在博客中谈到了如何在这里实现这一点。