我正在 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
类构造函数:
public Currency(Context context) {
thiscontext = context;
}
我在将参数 Context context 添加到构造函数后开始遇到此问题。
是什么原因造成的?我该如何解决?
是否可以序列化/反序列化具有参数的类?
最佳答案
您需要在 Currency 类中有一个空构造函数才能对其进行反序列化。
关于c# - 反序列化类时出现 "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069962/