1

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

  • 适用于 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 的默认构造函数(它没有明确定义的构造函数)。

任何帮助深表感谢。

4

2 回答 2

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();
        }
    }
}
于 2013-05-26T15:34:29.133 回答
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>

我在这里写了关于如何实现这一点的博客。

于 2013-06-14T13:06:31.870 回答