CodexBloom - Programming Q&A Platform

Visual Studio 2022 - Debugging 'TypeLoadException' When Using Custom Serialization with Newtonsoft.Json

👀 Views: 84 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
visual-studio json newtonsoft-json .net-6 C#

I'm trying to figure out I'm working on a project and hit a roadblock. I've been struggling with this for a few days now and could really use some help... This might be a silly question, but I'm working with a `TypeLoadException` when trying to deserialize JSON into my custom class using Newtonsoft.Json in a .NET 6 application. The behavior message states: `Could not load type 'MyNamespace.MyClass' from assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.` I've double-checked that the class is public and correctly defined in the specified namespace. My class looks like this: ```csharp namespace MyNamespace { public class MyClass { public string PropertyOne { get; set; } public int PropertyTwo { get; set; } } } ``` I'm deserializing the JSON like this: ```csharp string json = "{ \"PropertyOne\": \"Value\", \"PropertyTwo\": 123 }"; var result = JsonConvert.DeserializeObject<MyClass>(json); ``` This works fine in a different project targeting .NET Framework, but fails in my current .NET 6 project. I've tried cleaning and rebuilding the solution, ensuring all dependencies are restored, and even restarting Visual Studio. I've also checked the project settings to confirm that the target framework is correctly set to .NET 6. I suspect it might have something to do with how the assemblies are being loaded in .NET 6 or potentially an scenario with the Newtonsoft.Json version. I'm currently using version 13.0.1 of Newtonsoft.Json. Has anyone encountered this scenario or have any insights into what might be going wrong? For context: I'm using C# on Windows. Am I missing something obvious? Is there a better approach? What are your experiences with this? Has anyone dealt with something similar?