为什么Mono.Cecil HelloWorld示例会因异常而失败?

时间:2017-10-13 11:44:33

标签: f# mono.cecil

open System
open Mono.Cecil
open Mono.Cecil.Cil

let myHelloWorldApp = 
    AssemblyDefinition.CreateAssembly(
        new AssemblyNameDefinition("HelloWorld", new Version(1, 0, 0, 0)), "HelloWorld", ModuleKind.Console)

let module_ = myHelloWorldApp.MainModule

// create the program type and add it to the module
let programType = 
    new TypeDefinition("HelloWorld", "Program",
        Mono.Cecil.TypeAttributes.Class ||| Mono.Cecil.TypeAttributes.Public, module_.TypeSystem.Object)

module_.Types.Add(programType)

// add an empty constructor
let ctor = 
    new MethodDefinition(".ctor", Mono.Cecil.MethodAttributes.Public ||| Mono.Cecil.MethodAttributes.HideBySig
        ||| Mono.Cecil.MethodAttributes.SpecialName ||| Mono.Cecil.MethodAttributes.RTSpecialName, module_.TypeSystem.Void)

// create the constructor's method body
let il = ctor.Body.GetILProcessor()

il.Append(il.Create(OpCodes.Ldarg_0))

// call the base constructor
il.Append(il.Create(OpCodes.Call, module_.ImportReference(typeof<obj>.GetConstructor([||]))))

il.Append(il.Create(OpCodes.Nop))
il.Append(il.Create(OpCodes.Ret))

programType.Methods.Add(ctor)

// define the 'Main' method and add it to 'Program'
let mainMethod = 
    new MethodDefinition("Main",
        Mono.Cecil.MethodAttributes.Public ||| Mono.Cecil.MethodAttributes.Static, module_.TypeSystem.Void)

programType.Methods.Add(mainMethod)

// add the 'args' parameter
let argsParameter = 
    new ParameterDefinition("args",
        Mono.Cecil.ParameterAttributes.None, module_.ImportReference(typeof<string[]>))

mainMethod.Parameters.Add(argsParameter);

// create the method body
il = mainMethod.Body.GetILProcessor()

il.Append(il.Create(OpCodes.Nop))
il.Append(il.Create(OpCodes.Ldstr, "Hello World"))

let writeLineMethod = 
    il.Create(OpCodes.Call,
        module_.ImportReference(typeof<Console>.GetMethod("WriteLine", [|typeof<string>|])))

// call the method
il.Append(writeLineMethod)

il.Append(il.Create(OpCodes.Nop))
il.Append(il.Create(OpCodes.Ret))

// set the entry point and save the module
myHelloWorldApp.EntryPoint <- mainMethod

我从问题的答案中借用了the example并在F#中重写了它。当我尝试运行它时,我收到以下错误:

Unhandled Exception: System.TypeLoadException: Could not load type 'HelloWorld.Program' from assembly 'Hello
on=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'Main' has no implementation (no RVA).

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

我的猜测是问题是由以下行引起的:

// create the method body
il = mainMethod.Body.GetILProcessor()

在C#中,这会将Main方法的IL处理器分配给il变量,但在F#中,这只是一个导致false的相等测试 - 所以您要为Main方法生成的IL代码将添加到构造函数的上一个il处理器中。

你应该可以使用变量阴影来解决这个问题:

// create the method body
let il = mainMethod.Body.GetILProcessor()