// NOTE: ldarg.0 holds the "this" reference - ldarg.1, ldarg.2, and ldarg.3
// hold the actual passed parameters. ldarg.0 is used by instance methods
// to hold a reference to the current calling object instance. Static methods
// do not use arg.0, since they are not instantiated and hence no reference
// is needed to distinguish them.
ctorIL.Emit(OpCodes.Ldarg_0);
// Here, we wish to create an instance of System.Object by invoking its
// constructor, as specified above.
ctorIL.Emit(OpCodes.Call, objCtor);
// Now, we'll load the current instance ref in arg 0, along
// with the value of parameter "x" stored in arg 1, into stfld.
mthdIL.Emit(OpCodes.Ldarg_0);
switch (mthdName) {
case "GetX": mthdIL.Emit(OpCodes.Ldfld, xField);
break;
case "GetY": mthdIL.Emit(OpCodes.Ldfld, yField);
break;
case "GetZ": mthdIL.Emit(OpCodes.Ldfld, zField);
break;
}
mthdIL.Emit(OpCodes.Ret);
}
// Finally, we create the type.
pointType = pointTypeBld.CreateType();
// Let's save it, just for posterity.
myAsmBuilder.Save("Point.dll");
return pointType;
}
public static void Main() {
Type myDynamicType = null;
object aPoint = null;
Type[] aPtypes = new Type[] {typeof(int), typeof(int), typeof(int)};
object[] aPargs = new object[] {4, 5, 6};
// Call the method to build our dynamic class.
myDynamicType = DynamicPointTypeGen();
Console.WriteLine("Some information about my new Type '{0}':",
myDynamicType.FullName);
Console.WriteLine("Assembly: '{0}'", myDynamicType.Assembly);
Console.WriteLine("Attributes: '{0}'", myDynamicType.Attributes);
Console.WriteLine("Module: '{0}'", myDynamicType.Module);
Console.WriteLine("Members: ");
foreach (MemberInfo member in myDynamicType.GetMembers()) {
Console.WriteLine("-- {0} {1};", member.MemberType, member.Name);
}
Console.WriteLine("---");
// Let's take a look at the constructor we created.