0

如何在 .NET5 中加载 DynamicAssembly 使用 Emit?

    var loadContext = new MyAssemblyLoadContext();
    var assemby = loadContext.LoadFromAssemblyName(new AssemblyName("MyDynamicAssemby"));

    public class MyAssemblyLoadContext : AssemblyLoadContext
{
    public MyAssemblyLoadContext() : base(true)
    {
        this.Resolving += OnResolving;
    }

    protected override Assembly Load(AssemblyName assemblyName)
    {
        return null;
    }
    private Assembly OnResolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
    {
        var dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        var moduleBuilder = dynamicAssembly.DefineDynamicModule(assemblyName.Name);
        // Define some dynamic type
        return dynamicAssembly;
    }
}

我在 loadContext.LoadFromAssemblyName(...) 处收到错误

InvalidOperationException:已解析程序集的简单名称应与请求的程序集相同。

我错过了什么?任何帮助或示例代码都对我有用。

4

0 回答 0