0

I'm working on a project that dynamically compiles and executes a C# code, and we use the Npgsql library, so we are loading the Npgsql.dll as shown here, ie, we use AssemblyLoadContext after compiling the dynamic code:

Assembly assembly = 
AssemblyLoadContext.Default.LoadFromAssemblyPath(dll_path);

Where dll_path is the path of the Npgsql.dll file.

So far everything is fine, the problem is when we try to cast a IntPtr to a Npgsql object in the dynamically code, because the Npgsql object was firstly created in the C# project. Thus, we are facing the following error:

Unhandled exception. System.InvalidCastException: [A]NpgsqlTypes.NpgsqlPoint cannot be cast to [B]NpgsqlTypes.NpgsqlPoint.
Type A originates from 'Npgsql, Version=6.0.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' in the context 'Default' at location '/var/lib/DotNetEngine/src/csharp/bin/Release/net6.0/Npgsql.dll'.
Type B originates from 'Npgsql, Version=6.0.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' in the context 'Default' at location '/var/lib/DotNetEngine/src/csharp/bin/Release/net6.0/Npgsql.dll'.

As can be seen, it is as if the NpgsqlPoint type defined in the code is different from the same NpgsqlPoint type defined in the dynamic code, even both types being originated from the same DLL file.

Any help is appreciated!

Rosicley
  • 1
  • 1

0 Answers0