J
julia_beresford
Hi
I need to create the following class at runtime:
public class MyCollection : CollectionBase
{
public void Add(MyItem item)
{
List.Add(item);
}
public MyItem this[int index]
{
get
{
return (MyItem)List[index];
}
}
}
(the type MyType will also be of a type created at runtime with the
TypeBuilder)
I thought a good plan would be to create such a class and use the
ILDASM.exe tool on it to look at the IL generated. I got this (for
the Add method):
.method public hidebysig instance void
Add(string item) cil managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance class
[mscorlib]System.Collections.IList [mscorlib]
System.Collections.CollectionBase::get_List()
IL_0007: ldarg.1
IL_0008: callvirt instance int32
[mscorlib]System.Collections.IList::Add(object)
IL_000d: pop
IL_000e: ret
} // end of method Class1::Add
and the code i've tried to write looks a little something like this:
methodBuilder = typeBuilder.DefineMethod("Add",
MethodAttributes.Public, null, new Type[] { collectionType } );
ILout = methodBuilder.GetILGenerator();
ILout.Emit(OpCodes.Nop);
ILout.Emit(OpCodes.Ldarg, 0);
ILout.Emit(OpCodes.Call, ????????);
ILout.Emit(OpCodes.Ldarg, 1);
ILout.Emit(OpCodes.Callvirt,
typeof(System.Collections.IList).GetMethod("Add", new Type[]
{typeof(Object)}));
ILout.Emit(OpCodes.Pop);
ILout.Emit(OpCodes.Ret);
You can see I've had a few problems with the OpCodes.Call - can anyone
help me please fill in the gaps?
Also, how do you interpret the following (IL_0013: br.s
IL_0015) to code, in this example:
.method public hidebysig specialname instance string
get_Item(int32 index) cil managed
{
// Code size 23 (0x17)
.maxstack 2
.locals init ([0] string CS$1$0000)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance class
[mscorlib]System.Collections.IList
[mscorlib]System.Collections.CollectionBase::get_List()
IL_0007: ldarg.1
IL_0008: callvirt instance object
[mscorlib]System.Collections.IList::get_Item(int32)
IL_000d: castclass [mscorlib]System.String
IL_0012: stloc.0
IL_0013: br.s IL_0015
IL_0015: ldloc.0
IL_0016: ret
} // end of method Class1::get_Item
If anyone can help me i'd be very grateful.
Many thanks
Julia.
I need to create the following class at runtime:
public class MyCollection : CollectionBase
{
public void Add(MyItem item)
{
List.Add(item);
}
public MyItem this[int index]
{
get
{
return (MyItem)List[index];
}
}
}
(the type MyType will also be of a type created at runtime with the
TypeBuilder)
I thought a good plan would be to create such a class and use the
ILDASM.exe tool on it to look at the IL generated. I got this (for
the Add method):
.method public hidebysig instance void
Add(string item) cil managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance class
[mscorlib]System.Collections.IList [mscorlib]
System.Collections.CollectionBase::get_List()
IL_0007: ldarg.1
IL_0008: callvirt instance int32
[mscorlib]System.Collections.IList::Add(object)
IL_000d: pop
IL_000e: ret
} // end of method Class1::Add
and the code i've tried to write looks a little something like this:
methodBuilder = typeBuilder.DefineMethod("Add",
MethodAttributes.Public, null, new Type[] { collectionType } );
ILout = methodBuilder.GetILGenerator();
ILout.Emit(OpCodes.Nop);
ILout.Emit(OpCodes.Ldarg, 0);
ILout.Emit(OpCodes.Call, ????????);
ILout.Emit(OpCodes.Ldarg, 1);
ILout.Emit(OpCodes.Callvirt,
typeof(System.Collections.IList).GetMethod("Add", new Type[]
{typeof(Object)}));
ILout.Emit(OpCodes.Pop);
ILout.Emit(OpCodes.Ret);
You can see I've had a few problems with the OpCodes.Call - can anyone
help me please fill in the gaps?
Also, how do you interpret the following (IL_0013: br.s
IL_0015) to code, in this example:
.method public hidebysig specialname instance string
get_Item(int32 index) cil managed
{
// Code size 23 (0x17)
.maxstack 2
.locals init ([0] string CS$1$0000)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance class
[mscorlib]System.Collections.IList
[mscorlib]System.Collections.CollectionBase::get_List()
IL_0007: ldarg.1
IL_0008: callvirt instance object
[mscorlib]System.Collections.IList::get_Item(int32)
IL_000d: castclass [mscorlib]System.String
IL_0012: stloc.0
IL_0013: br.s IL_0015
IL_0015: ldloc.0
IL_0016: ret
} // end of method Class1::get_Item
If anyone can help me i'd be very grateful.
Many thanks
Julia.