Protected memory violation when executing method from dynamically loaded assembly

Godot Version

4.3

Question

I’m making a modding system for my game, where the modders write their code in C# and compile it to a DLL, which the game loads and executes methods from.

The relevant part of the compile method (returns an Assembly)

string dll = //the absolute path...
if (File.Exists(dll)) {
   return Assembly.LoadFrom(dll);
}

Then I invoke the method like this:

    private void ExecuteModMethod(Mod mod, string methodName, object[] methodParameters)
    {
        if (!useMods) return;
        Type type = mod.Assembly.GetType(mod.Name+".Main");
        if (type == null) return;
        MethodInfo method = type.GetMethod(methodName);
    
        mod.Instance = Activator.CreateInstance(type);
        if (method == null) return;
        method.Invoke(mod.Instance, methodParameters);
    }

When I try to invoke the _Ready method, (modders should name their init method after Godot’s one for consistensy), I get Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This is the only code file in the mod:

using Godot;


namespace TestMod
{
    public class Main
    {
        //Called when the game is loaded
        public void _Ready()
        {
            GD.Print("Hello from the mod!");
        }
    }
}

I don’t know much about DLLs and this sort of thing so any help is appreciated. I just need the error to go away because it stops execution of the application and so no method is invoked.

NOTES