我有一个C#代码,它将JArray对象 jsonArray (JSON.Net)转换为对象列表 jsonList (List ):

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]在: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.ThreadSafeStore 2[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密钥)[0x00000],位于:0处,
Newtonsoft.Json.Serialization.CachedAttributeGetter 1[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]()在0处位于GuiaTV.AgoraController.GetJSON()[0x00015]在[0x00000]
/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

最佳答案

似乎您正在为此开发基于触摸的应用程序,因此您需要用于Mono Touch的最新版本的NewtonSoft。可从here获取它。

如果要为现代UI构建它,则非常容易遵循Win 8模板或使用“任务”来启动线程。

尝试放入Try,Catch,Finally块以获取异常详细信息。

关于c# - 当我尝试将JArray转换为线程中的对象时出现System.TypeLoadException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15048409/

10-10 06:02