【问题标题】:Mono.Cecil create new instructionsMono.Cecil 创建新指令
【发布时间】:2014-09-15 12:12:28
【问题描述】:

我有一个小问题。 我正在尝试在我创建的方法中插入带有 Mono.Cecil 的指令集合。

Collection<Instruction> InstructionList = new Collection<Instruction>();

要添加一条没有操作数的普通指令,例如“Ret”或“ldarg.0”,我只需这样做:

InstructionList.Add(Instruction.Create(OpCodes.Ret));

但我在创建具有如下操作数的指令时遇到问题(图片):

http://puu.sh/bzWi8/710c8008df.png

谁能解释我如何使用这些操作数添加指令,例如 ldsfld 空字符串或 br.s 或 callvirt。

我不知道该怎么做。

我的尝试是这样的:

InstructionList.Add(Instruction.Create(OpCodes.Ldsfld, ModuleDef.Import(typeof(System.String))));

但这会引发异常:Mono.Cecil.dll 中发生了“System.ArgumentException”类型的未处理异常 附加信息:操作码

谁能解释我如何添加这些“更复杂”的指令?

【问题讨论】:

  • 你需要给它2个操作数,即一个类型和一个值
  • 这个值会是什么样子? (对不起,我对此不是很有经验)

标签: c# opcode mono.cecil operand


【解决方案1】:

InstructionList.Add(Instruction.Create(OpCodes.Ret));

应该是

InstructionList.Add(Instruction.Create(OpCodes.Ret, null));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多