System.TypeLoadException เมื่อฉันพยายามแปลง JArray เป็นวัตถุในเธรด

ฉันมีรหัส C# ที่แปลงวัตถุ JArray jsonArray (JSON.Net) เป็นรายการวัตถุ jsonList (List‹ MyClass >):

List<MyClass> jsonList = jsonArray.ToObject<List<MyClass>> ();

โค้ดด้านบนทำงานได้ดีเมื่อฉันรันบนเธรดหลัก แต่ถ้าฉันใส่โค้ดเดียวกันในเธรดอื่น จะเป็นดังนี้:

Thread t = new Thread(delegate() {
    List<MyClass> jsonList = jsonArray.ToObject<List<MyClass>> ();
});
t.Start();

ฉันได้รับข้อความแสดงข้อผิดพลาดต่อไปนี้: "System.TypeLoadException has been Thrown มีข้อผิดพลาดในการโหลดประเภทเกิดขึ้น"

ไม่มีใครมีความคิดว่าทำไมสิ่งนี้ถึงเกิดขึ้น? การติดตามสแต็กแบบเต็มอยู่ด้านล่าง ขอบคุณล่วงหน้า!

System.TypeLoadException: เกิดข้อยกเว้นการโหลดชนิด ที่ Newtonsoft.Json.Utilities.ThreadSafeStore2[System.Type,System.Type].AddValue (System.Type key) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Utilities.ThreadSafeStore2[System.Type,System.Type].Get (คีย์ System.Type) [0x00000] ใน :0 ที่ Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType (ประเภท System.Type) [0x00000] ใน :0 ที่ Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (ประเภท System.Type) [0x00000] ใน :0 ที่ Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (ICustomAttributeProviderattributeProvider) [0x00000] ใน : 0 ที่ Newtonsoft.Json.Utilities.ThreadSafeStore2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].AddValue (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Utilities.ThreadSafeStore2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].Get (คีย์ ICustomAttributeProvider) [0x00000] ใน: 0 ที่ Newtonsoft.Json.Serialization.CachedAttributeGetter1[Newtonsoft.Json.JsonContainerAttribute].GetAttribute (ICustomAttributeProvider type) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonContainerAttribute (System.Type type) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonObjectAttribute (System.Type type) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Linq.JToken.ToObject (System.Type objectType, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Linq.JToken.ToObject (System.Type objectType, Boolean isNullable) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Linq.JToken.ToObject (System.Type objectType) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Linq.JToken.ToObject[List1] () [0x00000] ใน: 0 ที่ GuiaTV.AgoraController.GetJSON () [0x00015] ใน /Users/vegidio/Documents/Dev/Xamarin/GuiaTV/GuiaTV/Controllers/AgoraController.cs:24 ที่ GuiaTV.AgoraScreen.m__2 () [0x0000d] ใน /Users/ vegidio/Documents/Dev/Xamarin/GuiaTV/GuiaTV/Views/AgoraScreen.cs:43 ที่ System.Threading.Thread.StartInternal () [0x0001d] ใน /Developer/MonoTouch/Source/mono/mcs/class/corlib/System. การทำเกลียว/Thread.cs:697


person vegidio    schedule 24.02.2013    source แหล่งที่มา
comment
ปัญหาของคุณอาจเกี่ยวข้องกับคำถามนี้: stackoverflow.com/ คำถาม/10112533/   -  person Mike Zboray    schedule 24.02.2013
comment
ขอบคุณมากที่ช่วยได้ เห็นได้ชัดว่ามี JSON.Net เวอร์ชันเฉพาะที่ใช้งานได้กับ MonoTouch   -  person vegidio    schedule 24.02.2013


คำตอบ (2)


ดูเหมือนว่าคุณกำลังพัฒนาแอปพลิเคชันระบบสัมผัส คุณต้องมี NewtonSoft สำหรับ Mono Touch รุ่นล่าสุด รับได้จาก ที่นี่.

หากคุณกำลังสร้างมันขึ้นมาสำหรับ UI ที่ทันสมัย ​​คุณสามารถทำตามเทมเพลต Win 8 หรือใช้งานเพื่อเริ่มต้นเธรดได้อย่างง่ายดาย

ลองใส่ Try, Catch, Finally block เพื่อรับรายละเอียดข้อยกเว้น

person Sant14    schedule 24.02.2013
comment
ขอบคุณ. ฉันไม่รู้ว่ามีพอร์ตเฉพาะของ JSON.Net เพื่อที่จะทำงานบน MonoTouch - person vegidio; 24.02.2013
comment
ถ้าคุณชอบคำตอบ คุณก็สามารถยอมรับมันได้ ขึ้นอยู่กับคุณ คำถามใดๆ เกี่ยวกับปัญหาที่คุณสามารถถามคำถามได้ จากนั้นมีเพียงเราเท่านั้นที่สามารถใช้สมองและเทคนิคการค้นหาอินเทอร์เน็ตเพื่อรับคำตอบ - person Sant14; 24.02.2013

หากคุณตั้งค่าเบรกพอยต์ไว้

List<MyClass> jsonList = jsonArray.ToObject<List<MyClass>> ();

และโฮเวอร์เบรกพอยต์นั้นเพื่อดูข้อยกเว้นภายใน มันบอกว่าอะไร?

เหตุผลที่ฉันถามคือเนื่องจากข้อยกเว้น TypeLoad จะส่งค่าว่างหรือส่งข้อความถึงคุณเกี่ยวกับข้อยกเว้นภายในซึ่งอาจช่วยคุณได้มากกว่าการติดตามสแต็กนี้เล็กน้อย

person Sipps    schedule 24.02.2013