我有一个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。发生了类型加载异常”。
有人知道为什么会这样吗?完整的堆栈跟踪如下。提前谢谢!
System.TypeLoadException:发生了类型加载异常。在 Newtonsoft.Json.Utilities.ThreadSafeStore
2[System.Type,System.Type].AddValue (System.Type key) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Utilities.ThreadSafeStore
2 [System.Type的,System.Type的]。获得 (System.Type键)[0x00000] in:0 at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType (System.Type类型)[0x00000] in:0 at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute [JsonContainerAttribute] (System.Type类型)[0x00000] in:0 at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute [JsonContainerAttribute] (ICustomAttributeProvider attributeProvider)[0x00000] in:0 at Newtonsoft.Json.Utilities.ThreadSafeStore2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].AddValue (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Utilities.ThreadSafeStore
2 [System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute]。获得 (ICustomAttributeProvider key)[0x00000] in:0 at 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[List
1]()[0x00000] in:0 at GuiaTV.AgoraController.GetJSON()[0x00015] in /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.Threading/Thread.cs:697
答案 0 :(得分:1)
您似乎正在开发基于触摸的应用程序,因此您需要最新版本的NewtonSoft for Mono Touch。从here获取它。
如果您正在为现代UI构建它,则可以非常轻松地按照Win 8模板或使用任务来启动线程。
尝试输入Try,Catch,Finally块以获取异常详细信息。
答案 1 :(得分:0)
如果在
上设置断点List<MyClass> jsonList = jsonArray.ToObject<List<MyClass>> ();
并将该断点留空以查看内部异常,它会说什么?
我问的原因是因为TypeLoad异常会抛出一个null,或者在内部异常上给你一条消息,它可以帮助你比这个堆栈跟踪更多。