cil - 在 VB.Net 中使用 System.Reflection.Emit.ILGenerator 调用 Random?

标签 cil reflection.emit ilgenerator

我正在用我自己的语言为 .Net 可执行文件生成输出...从我的语言翻译过来的操作码(称为“随机”)应该在特定范围内创建一个随机数。

我的代码的目标是使用 System.Reflection.Emit.ILGenerator 类生成随机数...为了了解 CIL 代码的外观我创建了一些 vb.net 代码:

Sub Main()
    Dim A As Random

    A = New Random

    Console.WriteLine(A.Next(100))
End Sub

哪个 ILDASM 报告为:

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       23 (0x17)
  .maxstack  2
  .locals init ([0] class [mscorlib]System.Random A)
  IL_0000:  nop
  IL_0001:  newobj     instance void [mscorlib]System.Random::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.s   100
  IL_000a:  callvirt   instance int32 [mscorlib]System.Random::Next(int32)
  IL_000f:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0014:  nop
  IL_0015:  nop
  IL_0016:  ret
} // end of method Main::Main

我可以使用 ILGenerator.Emit 方法重现一切;除了行 IL_0001(“newobj instance void [mscorlib]System.Random::.ctor()”)...

希望我没有用太多信息淹没任何人。但我认为在描述对我来说似乎很复杂的问题时,最好是详细一些。

我终于有了到目前为止生成的代码:

 Sub EmitRandom()
    Dim NewRandom As New Random
    Dim stringtype As Type = GetType(System.Random)

    Dim paramtypes() As Type = {GetType(Integer)}, blankparams() As Type = {}
    'Dim RandomMethod = stringtype.GetMethod("New", paramtypes)

    m_ILGen.Emit(OpCodes.Newobj, New Random().GetType)

    EmitStoreInLocal(tempVariableRnd)
    EmitLoadLocal(tempVariableRnd)

    m_ILGen.Emit(OpCodes.Callvirt, stringtype.GetMethod("Next", paramtypes))
End Sub

发出以下代码:

.
.
.
IL_0073:  newobj     [mscorlib]System.Random
IL_0078:  stloc.2
IL_0079:  ldloc.2
IL_007a:  callvirt   instance int32 [mscorlib]System.Random::Next(int32)
.
.
.

我已经尝试过的事情:

  • 想出一个方法来指向 IL_Gen.Emit(OpCodes.NewObj, ... ctor())... 不知道怎么做。

  • 想出一种指向 New() 的方法——因为这似乎就是 .ctor() 的意思……New 只能用作初始化程序。

    <
  • 在我想出更好的发射方式之前,请先禁用随机功能。

这个问题对我来说似乎很难,但我知道有人比我更容易理解代码生成和 MSIL,并且愿意指出答案。

谢谢你的时间,

多米尼克

最佳答案

您需要使用 ConstructorInfo:

 m_ILGen.Emit(OpCodes.Newobj, GetType(Random).GetConstructor(Type.EmptyTypes))

此外 - 不需要从本地存储和加载。你真的只想要 new Random().Next(100) 的等价物,对吧?...在​​这种情况下,从本地存储和加载永远不会发生。

关于cil - 在 VB.Net 中使用 System.Reflection.Emit.ILGenerator 调用 Random?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318139/

相关文章:

c# - 在 C# 中使用枚举索引数组

C# 动态构建匿名对象

c# - Reflection.Emit 优于 GetValue 和 SetValue :S

c# - 不一致的 Reflection.Emit 支持 Mono?

c# - Object.Equals(Object, Object) 处的 NullReferenceException

c# - 将一个对象属性值传输到另一个对象

c# - VS Code 指标如何计算

cil - MSIL 引用手册

c# - ILGenerator实例化对象并调用实例方法

.NET CIL 调用还是 CallVirt?