在带有.NET标准库的Xamarin.Android上,JsonSerializationException找不到构造函数 [英] JsonSerializationException 'Unable to find a constructor' on Xamarin.Android with .NET Standard Library
本文介绍了在带有.NET标准库的Xamarin.Android上,JsonSerializationException找不到构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了Xamarin Native UI for Android应用程序,并使用Newtonsoft.Json
为API调用和数据反序列化API数据创建了不同的类库。
.NET Standar 2.0
。
因为我已经添加了对控制台应用程序的引用,所以它工作正常,但是相同的引用,我已经在Android项目中添加了它的抛出错误。
<2-2]>
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.
根据错误消息,我已使用属性JsonConstructor
作为类的默认构造函数。
示例:
public class TestClass
{
[JsonConstructor]
public TestClass()
{
}
}
推荐答案
因为PReserve veAttribute需要其中的Mono.android.dll或‘Xamarin.iOS.dll’引用,但我的类库对这两个引用是通用的,这就是为什么不可能
将PreserveAttribute
类添加到类库并使用该属性,因为Mono链接器只使用属性的"名称",而不使用命名空间/类名...
public sealed class PreserveAttribute : Attribute
{
public bool AllMembers;
public bool Conditional;
public PreserveAttribute (bool allMembers, bool conditional)
{
AllMembers = allMembers;
Conditional = conditional;
}
public PreserveAttribute ()
{
}
}
然后在您的JSON模型/类上使用该属性:
[Preserve(AllMembers = true)]
public class TestClass
{
~~~
这篇关于在带有.NET标准库的Xamarin.Android上,JsonSerializationException找不到构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文