我已将 Xamarin Native UI 用于 Android 应用程序,并为 API 调用和数据创建了不同的类库,并使用反序列化 api 数据Newtonsoft.Json
.
该类库 Target Framework 是.NET Standar 2.0
.
由于我已添加对控制台应用程序的引用,因此它工作正常,但我在 Android 项目中添加了相同的引用,引发错误。
**Error Details**
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
用于类的默认构造函数。
Example:
public class TestClass
{
[JsonConstructor]
public TestClass()
{
}
}
由于 PreserveAttribute 需要 Mono.Android.dll 或“Xamarin.iOS.dll”引用,但我的类库对于两者都是通用的,这就是为什么不可能
Add a 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
{
~~~
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)