尝试将JArray转换为线程中的对象时出现System.TypeLoadException

本文关键字:对象 TypeLoadException System JArray 转换 线程 | 更新日期: 2023-09-27 18:00:52

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

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].Get(System.Type键([00x00000]位于:0Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType(System.Type类型([00x00000]位于:0Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute](System.Type类型([00x00000]位于:0Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute](ICustomAttributeProvider attributeProvider([0x0000]位于:0Newtonsoft.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密钥([0x0000]位于:0Newtonsoft.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](([0x0000]in:0 at GuiaTV.AgoraController.GetJSON(([00x00015]in/Users/vegidio/Documents/Dev/Xamarin/GuiaTV/GuiaTV/Controllers/AgoraController.cs:24位于中的GuiaTV.AgoraScreen.m__2(([0x000d]/Users/vegidio/Documents/Dev/Xamarin/GuiaTV/GuiaTV/Views/AgoraScreen。cs:43位于中的System.Threading.StartInternal(([0x001d]/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/Thread.cs:697

尝试将JArray转换为线程中的对象时出现System.TypeLoadException

您似乎正在为此开发一个基于触摸的应用程序,您需要最新版本的NewtonSoft for Mono touch。从这里获取。

如果你是为一个现代的UI构建它,它很容易遵循Win 8模板或使用Task来启动线程。

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

如果在上设置断点

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

并将该断点悬停以查看内部异常,它会显示什么?

我之所以这么问,是因为TypeLoad异常会抛出一个null,或者在内部异常上给你一条消息,这可能比这个堆栈跟踪更能帮助你。