在VB.Net中使用System.Reflection.Emit.ILGenerator调用Random?
我正在用我自己的语言为 .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 只能用作初始值设定项。
只需禁用随机函数,直到我能想出更好的发射方式。
这个问题对我来说似乎很难,但我知道有人比我更容易理解代码生成和 MSIL,并且愿意指出答案。
感谢您抽出时间,
多米尼克
I'm generating output for a .Net executable from my own language... the opcode (called 'Random') that's being translated from my language is supposed to create a random number within a specific range.
The goal for my code is to produce the random number using System.Reflection.Emit.ILGenerator class... to understand how the CIL code looks I've created some vb.net code:
Sub Main()
Dim A As Random
A = New Random
Console.WriteLine(A.Next(100))
End Sub
Which ILDASM reports as:
.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
I can reproduce everything using the ILGenerator.Emit method; except the line IL_0001 ("newobj instance void [mscorlib]System.Random::.ctor()")...
Hopefully I haven't overwhelmed anyone with too much information. But I figure it's better to be verbose when describing a problem that seems complex to me.
Finally I have the code that I've produced so far:
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
Which emits the following code:
.
.
.
IL_0073: newobj [mscorlib]System.Random
IL_0078: stloc.2
IL_0079: ldloc.2
IL_007a: callvirt instance int32 [mscorlib]System.Random::Next(int32)
.
.
.
Things that I've tried already:
Coming up with a way to point IL_Gen.Emit(OpCodes.NewObj, ... ctor())... can't figure out how.
Coming up with a way to point to New() - since that seems to be what .ctor() is... New can't be used as anything but an initializer.
Just disabling the Random function until I can come up with a better way of emitting.
The problem seems hard to me but I know there's someone out there who understands code generation and MSIL more easily than I do and who is willing to point out an answer.
Thanks for your time,
Dominick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 ConstructorInfo:
另外 - 不需要从本地存储和加载。你真的只想要相当于 new Random().Next(100) 的东西,对吧?...在这种情况下,从本地存储和加载永远不会发生。
You need to use the ConstructorInfo:
Also - the storing and loading from local are unnecessary. You really just want the equivalent of new Random().Next(100), right?...in which case storing and loading from a local never happens.