All pastes #50454 Raw Edit

Something

public text v1 · immutable
#50454 ·published 2006-04-21 19:23 UTC
rendered paste body
using System;
using System.Reflection;
using System.Reflection.Emit;

class T {
#if BROKEN
        private static void Dispatch () {
#else
        public static void Dispatch () {
#endif
                Console.WriteLine ("Dispatched");
        }

        static void Main (string [] args) {
                AssemblyBuilder builder;
                ModuleBuilder module;
                AssemblyName name;
                TypeBuilder type;
                MethodBuilder method;
                ILGenerator ilg;
                Type compiledtype;

                name = new AssemblyName ();
                name.Name = "TheName";
                builder = AppDomain.CurrentDomain.DefineDynamicAssembly (name, AssemblyBuilderAccess.Run, null, null, null, null, null, true);
                module = builder.DefineDynamicModule ("TheModule");
                type = module.DefineType ("TheType");
                method = type.DefineMethod ("TheMethod", MethodAttributes.Public | MethodAttributes.Static, typeof (void), new Type [] {});
                ilg = method.GetILGenerator ();

#if BROKEN
                ilg.Emit (OpCodes.Call, typeof (T).GetMethod ("Dispatch", BindingFlags.NonPublic | BindingFlags.Static, null, new Type [] {}, null));
#else
                ilg.Emit (OpCodes.Call, typeof (T).GetMethod ("Dispatch", BindingFlags.Public | BindingFlags.Static, null, new Type [] {}, null));
#endif
                ilg.Emit (OpCodes.Ret);

                compiledtype = type.CreateType ();

                compiledtype.InvokeMember ("TheMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new Type [] {});
        }
}