android - Xamarin Android - 链接器和 ServiceStack.Text

标签 android xamarin xamarin.android servicestack xamarin-linker

我在让应用程序使用完整链接时遇到问题。这是我的设置(我的程序集名称已更改):

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

非常感谢任何帮助。

最佳答案

确实是 ServiceStack.Text 本身被链接器弄乱了。 JsonReader<T>.GetParseFn()通过反射访问,显然导致了它的遗漏。

我已经取消了 Linker Description XML,并添加了一个如下所示的类:

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

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

关于android - Xamarin Android - 链接器和 ServiceStack.Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759289/

相关文章:

java - 如何摆脱“Activity 已泄漏服务连接”错误

xaml - ListView 不触发 ItemTapped

android - xamarin 应用程序替换 Google Play 商店中的原生 Android 应用程序

ios - 只有在将方向更改为横向并返回纵向后才能正确显示纵向布局

xamarin.android - 在 Xamarin.Forms 内容页面中使用日期选择器控件时未处理的 WindowManagerBadTokenException

c# - 无法重新膨胀 fragment 对话框 Xamarin 和 C#

android - 线程和处理程序

python - 如何从 Python 脚本中检测 Android 操作系统?

android - Ionic/Cordova 和 Android 64 位要求

c# - 更改选取器 XAMARIN 中的颜色线