0

I use Assembly.LoadFrom to dynamically load assemblies and I use Activator.CreateInstance to create objects from types referring to those assemblies. When serializing these objects I use the TypeNameHandling setting from Newtonsoft.Json to make sure that the object restores to it's correct type when deserializing.

This is my serialization method:

Dim jsonSerializerSettings As New JsonSerializerSettings() With {
    .TypeNameHandling = TypeNameHandling.All,
    .TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
    }
Dim JsonValue as string = JsonConvert.SerializeObject(ObjectToSerialize, jsonSerializerSettings)

And this is my deserialization method:

Dim jsonSerializerSettings As New JsonSerializerSettings() With {
    .TypeNameHandling = TypeNameHandling.All,
    .TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
    }
Dim NewObject = JsonConvert.DeserializeObject(Of T)(JsonValue, jsonSerializerSettings)

The problem is that on deserialization Visual Studio throws an Could not load assembly error despite the assembly being loaded. When looping through all loaded assemblies the correct assembly is clearly visible. While searching for a solution I found this StackOverflow post describing the exact same problem, but the solution does not work for me. I tried setting the TypeNameAssemblyFormatHandling to Full but it throws the same error. There were lots of changes to json.net in the last 9 years so the solution may have changed during that time.

What's even weirder is that after the error is thrown the deserialization does not fail. The object is actually deserialized as the expected type as if there where no problems. Also, the error is not captured by my application as an Exception. It almost looks like Visual Studio somehow fails and throws a false error. This is a screenshot of the error:

Exception

How to get rid of the error? Or is it safe to ignore this type of error?

Martin
  • 1,184
  • 8
  • 24

0 Answers0