替换 .NET Core 中的 AssemblyBuilder.DefineDynamicAssembly

Replacement of AssemblyBuilder.DefineDynamicAssembly in .NET Core

如何将以下代码移植到 .NET Core?

AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(
                    Guid.NewGuid().ToString()),
                    AssemblyBuilderAccess.RunAndSave);

可能吗?

将此添加到您的 project.json

"dependencies": {
    "System.Reflection.Emit": "4.0.1"
 },

并使用:

AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()),
            AssemblyBuilderAccess.Run);
目前不支持

AssemblyBuilderAccess.RunAndSave - link to source.


对于新的 .csproj 项目,使用:

<ItemGroup>
  <PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
</ItemGroup>