C#C
C#8mo ago
peppy

``Type.IsAssignableFrom`` returning false unexpectedly in plugin-loading scenario

This is a plugin framework. The "base" DLL with a plugin class that is subclassed by plugin DLLs is located in {ROOT}/bin/fhcore.dll, while plugins are located in {ROOT}/modules/{PLUGIN_NAME}/{PLUGIN_NAME.dll}.

This plugin framework is loaded into the application process by .NET hosting, if that is somehow meaningful.

All plugins have project references with <Private>false</Private> and <ExcludeAssets>runtime</ExcludeAssets>, ex.
        <ProjectReference Include="..\core\Fahrenheit.Core.csproj">
            <Private>false</Private>
            <ExcludeAssets>runtime</ExcludeAssets>
        </ProjectReference>


I first ensure bin/fhcore.dll is loaded into AssemblyLoadContext.Default.

Then for each plugin, I instantiate an AssemblyLoadContext as such:
public class FhLoadContext(string dll_path) : AssemblyLoadContext {
    private readonly AssemblyDependencyResolver _resolver = new AssemblyDependencyResolver(dll_path);

    protected override Assembly? Load(AssemblyName assembly_name) {
        string? assembly_path = _resolver.ResolveAssemblyToPath(assembly_name);
        return assembly_path != null ? LoadFromAssemblyPath(assembly_path) : null;
    }

    protected override nint LoadUnmanagedDll(string dll_name) {
        string? dll_path = _resolver.ResolveUnmanagedDllToPath(dll_name);
        return dll_path != null ? LoadUnmanagedDllFromPath(dll_path) : nint.Zero;
    }
}


I then LoadFromAssemblyPath the plugin into the context, and afterwards:
  • I clearly see that only the plugin itself (and any non-plugin-core dependencies thereof) are in the FhLoadContext
  • A duplicate reference to the core did not sneak in.
Yet, typeof(BaseModule).IsAssignableFrom(plugin_type) still returns
false
for a given public class PluginModule : BaseModule in the plugin DLL. Why could that be?
Was this page helpful?