f# - 有没有办法从 F# 代码运行 ILASM?

标签 f# nunit cil ilasm

我正在制作一个从 Java(的一小部分)到 F# 中的 CIL (MSIL) 的编译器,我正在考虑为实际编译部分编写一些单元测试。有没有一种方法可以在生成的 IL 代码上运行 ilasm 并从单元测试运行 .exe 文件?我正在使用 NUnit。

具体来说,有没有办法做到以下几点?

[<Test>]
member this.TestSimpleMain () = 
            let fileName = __SOURCE_DIRECTORY__.Replace("code", "examples\SimpleMain.cil")
            // run ilasm on fileName -> should produce ...\SimpleMain.exe
            let actualResult = // run ...\SimpleMain.exe file
            Assert.That(actualResult , Is.EqualTo("Program returned value 5!")))

最佳答案

如果你想使用ilasm确保以 IL 汇编格式(CIL 的文本表示)创建 SimpleMain.cil。 然后你应该从你的 il 文件创建 PE (exe/dll) 文件。

这里是带有输出和退出代码验证的 nunit 测试示例:

[<Test>]
member this.TestSimpleMain() =
    // Code that creates SimpleMain.il

    let p1 = new System.Diagnostics.Process()
    p1.StartInfo.FileName <- "C:/Windows/Microsoft.NET/Framework64/v4.0.30319/ilasm.exe"
    p1.StartInfo.Arguments <- "SimpleMain.il /exe /output=SimpleMain.exe"
    p1.Start()
    p1.WaitForExit()

    let p2 = new System.Diagnostics.Process()
    p2.StartInfo.FileName <- "SimpleMain.exe"
    p2.StartInfo.RedirectStandardOutput <- true // For output verification
    p2.Start()
    p2.WaitForExit()

    let resultA = p2.ExitCode // For exit code verification
    let resultB = p2.StandardOutput.ReadToEnd() // For output verification
    Assert.That(result, Is.EqualTo(expectedResult))

关于f# - 有没有办法从 F# 代码运行 ILASM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63364500/

相关文章:

python - F#中如何处理算术运算溢出异常?

f# - 在 F# 中将函数作为参数传递

nunit - NUnit 是否具有从另一个单元调用测试的内置方式?

c# - C# IL 在调用 C++ DLL 时如何通过 Ref 取回参数

.net - 非常简单的 F# 表单锁定键盘输入

f# - 为什么要使用向后管道运算符而不是函数链接?

.net - 在实现接口(interface)但使用基类的成员实现的 .NET 中创建 DynamicType

c# - 这条IL指令中的-2是什么意思?

c# - 二维对象数组返回类型 - NSubstitute

c# - NUnit:在断言中使用对象之前,我应该检查对象是否不为空吗?