我有一个 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 throwed. A type load exception has occurred”。
有人知道为什么会这样吗?完整的堆栈跟踪如下。提前致谢!
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].Get (System.Type key) [0x00000] in :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](ICustomAttributeProvider attributeProvider)[0x00000] 在:0 在 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].Get (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] 在 /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 在 /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/Thread.cs:697 中的 System.Threading.Thread.StartInternal () [0x0001d]