I am creating output for the .Net executable from my own language ... the operation code (called "Random"), which translates from my language, should produce a random number in a certain range.
The purpose of my code is to create a random number using the System.Reflection.Emit.ILGenerator class ... to understand what the CIL code looks like, I created some vb.net code:
Sub Main()
Dim A As Random
A = New Random
Console.WriteLine(A.Next(100))
End Sub
Which ILDASM reports:
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
.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
}
I can reproduce everything using the ILGenerator.Emit method; except for the line IL_0001 ("newobj instance void [mscorlib] System.Random ::. ctor ()") ...
Hopefully I did not overfill anyone with too much information. But I think it's better to describe a problem that seems complicated to me.
, , :
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()... -, .
Random, .
, , - , MSIL, , , .
,