public static void Main() { MyOverload mo = new MyOverload(); for (int i = 0; i < 3; i++) { Console.WriteLine("mo[{0}] = {1}", i, mo[i]); } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { { Console.WriteLine("mo[{0}, {1}] = {2}", i, j, mo[i, j]); } } } }
public static Type OverloadControl(Type t) { if (!IsAControl(t)) { return(t); } if (overloads != null) { foreach (MyOverload overload in overloads) { if (t == overload.t) { return(overload.new_type); } } } { // create a dynamic assembly and module AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "HelloWorld"; AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder module; module = assemblyBuilder.DefineDynamicModule("HelloWorld.dll"); // create a new type to hold our Main method TypeBuilder typeBuilder = module.DefineType( t.Name, TypeAttributes.Public | TypeAttributes.Class); //DynamicMethod. // create the Main(string[] args) method MethodBuilder methodbuilder = typeBuilder.DefineMethod( "a", MethodAttributes.Family, CallingConventions.HasThis, typeof(void), new Type[] { typeof(PaintEventArgs) }); //Type t2 = changeID.DeclaringType; // generate the IL for the Main method ILGenerator ilGenerator = methodbuilder.GetILGenerator(); // Push the current value of the id field onto the // evaluation stack. It's an instance field, so load the // instance of Example before accessing the field. ilGenerator.Emit(OpCodes.Ldarg_0); //ilGenerator.Emit(OpCodes.Ldfld, null); // Load the instance of Example again, load the new value // of id, and store the new field value. ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); //ilGenerator.Emit(OpCodes.Stfld, fid); // The original value of the id field is now the only // thing on the stack, so return from the call. ilGenerator.Emit(OpCodes.Ret); // bake it Type helloWorldType = typeBuilder.CreateType(); assemblyBuilder.Save("Helloworld.dll"); } { MethodInfo mi1 = typeof(osalot).GetMethod("BeginControlPaint"); MethodInfo mi_extend = typeof(osalot).GetMethod("ExtendListbox"); ConstructorInfo mi_object_ctor = t.GetConstructor(Type.EmptyTypes); MethodInfo mi_scale_event_handler = typeof(osalot).GetMethod("ScaleEventHandler"); MethodInfo mi2 = t.GetMethod("OnPaint", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo mi3 = typeof(osalot).GetMethod("EndControlPaint"); { if (ab == null) { ab = AppDomain.CurrentDomain.DefineDynamicAssembly(overload_assembly , AssemblyBuilderAccess.RunAndSave); } if (mb == null) { mb = ab.DefineDynamicModule("MyMetaAssembly.dll"); } TypeAttributes ta = TypeAttributes.Class | TypeAttributes.Public; TypeBuilder tb = mb.DefineType("MySubclassOf" + t.Name, ta, t); //MethodInfo mi_body; //MethodInfo mi_decl; //tb.DefineMethodOverride( mi_body, mi_decl ); //MethodAttributes.Public | MethodBuilder mbM = tb.DefineMethod("OnPaint" , MethodAttributes.Family | MethodAttributes.ReuseSlot | MethodAttributes.Virtual | MethodAttributes.HideBySig , CallingConventions.HasThis , typeof(void) , new Type[] { typeof(PaintEventArgs) }); ILGenerator il = mbM.GetILGenerator(); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi1); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi2); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi3); il.Emit(OpCodes.Ret); mbM = tb.DefineMethod("ScaleEventHander" , MethodAttributes.Family | MethodAttributes.ReuseSlot | MethodAttributes.HideBySig , CallingConventions.HasThis , typeof(void) , new Type[] { typeof(Canvas) }); il = mbM.GetILGenerator(); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi1); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi_scale_event_handler); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Call, mi3); il.Emit(OpCodes.Ret); ConstructorBuilder cbM = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.HasThis, Type.EmptyTypes); il = cbM.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Call, mi_object_ctor); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Call, mi_extend); il.Emit(OpCodes.Ret); Type result_type = tb.CreateType(); // save the newly created type so we can re-use it if multiply referenced. { MyOverload overload = new MyOverload(); MethodInfo[] meths2 = result_type.GetMethods(); { foreach (MethodInfo mi in meths2) { Log.log(mi.ToString()); } } overload.t = t; overload.new_type = result_type; overloads.Add(overload); } try { ab.Save("MyMetaAssembly.dll"); //ab.sa } catch (Exception ok) { } finally { // wahtever. } return(result_type); } } return(null); }