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

问题描述

我正在使用F#从Java(小的子集)到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))

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...