在运行时编译 .IL C#

问题描述

我不知道如何在运行时编译 IL 代码。 我使用 .NET core 3.1,我可以生成一个包含

的字符串
.assembly extern mscorlib {}
.assembly Hello {}
.module Hello.exe

.class Hello.Program
extends [mscorlib]System.Object
{
    .method static void Main(string[] args)
    cil managed
    {
        .entrypoint

        ldstr "H"
        call void[mscorlib]
            System.Console::
        Write(string)

        ldstr "i"
        call void[mscorlib]
            System.Console::
        Write(string)

        ret
    }
}

但是我如何编译文件呢?我想在运行时做。 (忽略错误的 IL 代码

解决方法

所以,我找到了方法。我从我的 .net 框架文件夹中复制了 ilasm.exe 和 fusion.dll,然后将字符串保存到一个文件中,然后我使用 ProcessInfo 运行 ilasm file。然后输出我想要的exe。

如果有人想知道,ilasm.exe 位于 C:\Windows\Microsoft.NET\Framework\Verison 如果您将版本替换为框架版本,例如 v4.0.30319。

感谢任何试图提供帮助的人!