gpt4 book ai didi

c# - 反序列化错误 - 值不能为空/未定义无参数构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:54 24 4
gpt4 key购买 nike

我有一个产品编辑页面,允许您添加“相关项目”。当访问页面以编辑产品时,它会在服务器上序列化其初始相关项目并存储在隐藏字段中。当您添加相关项目时,我使用 $.ajax() 在 javascript/jQuery 中检索项目数组. $.ajax()正在调用一个返回对象列表的服务器端方法,如下所示:

Fyodor.JSON.SearchResult r = new Fyodor.JSON.SearchResult();
DataSet ds = retrieveData();

foreach (DataRow row in ds.Tables[0].Rows)
{
JSON.Product i = new JSON.Product();
//set all Product data in `i`

if (!string.IsNullOrEmpty(i.PartNumber))
{
r.Results.Add(i);
}
}
//set r.TotalPageCount
return r;

当我将这个项目数组放回我的 javascript 时,每个项目都给了一个成员 __type等于 JSON.Product .这__type最初序列化项目时,成员不存在。我用 JSON.stringify(my_arrar_of_items)序列化新集合并存储回隐藏字段。
当我保存产品并检索隐藏字段的值并尝试反序列化时使用

JavaScriptSerializer s = new JavaScriptSerializer();
relatedItems = s.Deserialize<List<JSON.Product>>(hiddenField.Value);

我得到一个错误:

[ArgumentNullException: Value cannot be null. Parameter name: type] System.Activator.CreateInstance(Type type, Boolean nonPublic) +9635174 System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +431 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +199 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +141 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +231 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +80 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +44 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +74 Shop.UI.Admin.Products.Detail.CreateObject(Product& item) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:720 Shop.UI.Admin.Products.Detail.Save() in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:356 Shop.UI.Admin.Products.Detail.SaveClick(Object sender, EventArgs e) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:121 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

我确定错误是由于这个 __type 引起的成员。我怎样才能反序列化这个字符串?或者如何使用 $.ajax() 取回我的元素而不是该死的__type成员(member)?

我试过但没有用的东西:在我读到的一篇 SO 帖子中(我现在找不到了),它说要给你正在序列化的对象的构造函数一个访问级别 protected internal它不会添加 __type成员。该类实际上没有构造函数或方法,所以我创建了一个具有该访问级别的类,果然 __type成员(member)不在。但是现在我遇到了一个新错误:

[MissingMethodException: No parameterless constructor defined for type of 'Shop.JSON.Product'.] System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +582589 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.ObjectConverter.AddItemToList(IList oldList, IList newList, Type elementType, JavaScriptSerializer serializer, Boolean throwOnError) +87 System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList) +674 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +112 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +66 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +74 Shop.UI.Admin.Products.Detail.CreateObject(Product& item) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:720 Shop.UI.Admin.Products.Detail.Save() in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:356 Shop.UI.Admin.Products.Detail.SaveClick(Object sender, EventArgs e) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:121 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

我不明白,因为我创建的新构造函数无参数的。我做错了什么?

最佳答案

这条线是...

JSON.Product i = new JSON.Product();

在与您创建的构造函数相同的程序部分,还是在派生类中?如果不是,那就可以解释为什么它找不到构造函数。

来自 DotNetPerls : [protected internal] 表示内部可访问性(该程序的所有部分都可以使用该成员)和 protected 可访问性(所有派生类都可以使用该成员)。

关于c# - 反序列化错误 - 值不能为空/未定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11148644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com