C# &引用;Newtonsoft.Json.JsonSerializationException找不到用于类型的构造函数;反序列化类时出错

C# &引用;Newtonsoft.Json.JsonSerializationException找不到用于类型的构造函数;反序列化类时出错,c#,android,json,xamarin,deserialization,C#,Android,Json,Xamarin,Deserialization,我正在XamarinAndroid中开发一个应用程序。我有一个启动屏幕,其中我序列化了一个类,并使用Intent将其传递给MainActivity。当我尝试在MainActivity中对其进行反序列化时,会收到一条错误消息: "SerializationException unable to find constructor to use for types" 序列化: void LoadData() { Currency currency = new Currency(this);

我正在
Xamarin
Android中开发一个应用程序。我有一个启动屏幕,其中我序列化了一个类,并使用
Intent
将其传递给
MainActivity
。当我尝试在
MainActivity
中对其进行反序列化时,会收到一条错误消息:

"SerializationException unable to find constructor to use for types" 
序列化:

void LoadData() {
    Currency currency = new Currency(this);
    intent = new Intent(this, typeof(MainActivity));
    intent.PutExtra("currency", Newtonsoft.Json.JsonConvert.SerializeObject(currency));
    StartActivity(intent);
}
cur = Newtonsoft.Json.JsonConvert.DeserializeObject<Currency> (Intent.GetStringExtra("currency")); // I get the error here
反序列化:

void LoadData() {
    Currency currency = new Currency(this);
    intent = new Intent(this, typeof(MainActivity));
    intent.PutExtra("currency", Newtonsoft.Json.JsonConvert.SerializeObject(currency));
    StartActivity(intent);
}
cur = Newtonsoft.Json.JsonConvert.DeserializeObject<Currency> (Intent.GetStringExtra("currency")); // I get the error here
在向构造函数添加参数
Context
后,我开始遇到这个问题。 这是什么原因造成的?我怎样才能解决它?
是否可以序列化/反序列化具有参数的类?

您需要在Currency类中有一个空构造函数才能对其进行反序列化。

另一个可能导致此问题的原因是,如果您在android项目中启用了完全链接。您需要使用

[Runtime.Preserve(AllMembers = true)]
public class Currency
{}

那么,您希望反序列化传递给该构造函数的值是什么?为了反序列化,您真的需要那个参数吗?好的,Currency类的默认ctor不存在,这就是为什么您会得到这个错误;如果你是一个默认的ctor(没有参数),我相信你不会得到这个issue@JonSkeet抱歉,我没听清你的问题,你是说我的类中是否需要该参数?@rt2800这是否意味着我无法序列化/反序列化具有参数的类?那么你如何使用该参数?为什么你需要一个上下文?不是真的,你可以有这样的解决方案:你几乎在这里得到了正确的答案。。。这是选项=>build上的链接类型iOS@Daniel是的,Ios也有类似的行为。我写Android是因为这个问题是针对Android的。它也可以应用于Ios。你是对的,这个问题似乎也是由最新的Xamarin指南引起的,该指南使用Ios应用程序中不推荐的API启用完整链接以绕过Xamarin.Forms。