Xamarin Android - Linker和ServiceStack.Text

时间:2013-05-26 12:34:55

标签: android linker xamarin.android servicestack xamarin

我无法让应用程序使用完全链接。这是我的设置(我的程序集名称已更改):

  • Mono for Android 4.4.54
  • Android应用程序( MyApp
  • 平台无关的库( MyLib

我正在尝试使用ServiceStack.Text从JSON文本中反序列化类型(Person)。仅链接SDK程序集时,它可以正常工作。

我没有在 MyLib 中添加链接器包含属性,而是在 MyApp 中使用 LinkDescription XML文件。它看起来像这样:

<?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实例为空。

从输出看来,无法找到Person的默认构造函数(它没有明确定义的构造函数)。

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:1)

确实是ServiceStack.Text本身被链接器搞砸了。通过反思访问JsonReader<T>.GetParseFn(),显然导致其遗漏。

我已经废除了链接器描述 XML,我添加了一个类似这样的类:

using ServiceStack.Text.Common;
using ServiceStack.Text.Json;

namespace MyApp
{
    public class LinkerInclude
    {
        public ParseStringDelegate IncludeJsonReader<T>()
        {
            return JsonReader<T>.GetParseFn();
        }
    }
}

答案 1 :(得分:1)

最后,我实际上发现了更多我无法通过前一个答案中的代码方法解决的问题。这是因为链接器省略的某些类是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>

我在博客中介绍了如何实现这一目标here