Results 1 to 2 of 2

Thread: OT: DynamicMethod and IL Generation

  1. #1
    Join Date
    May 2006
    Location
    East Brunswick, NJ, USA
    Posts
    27

    Default OT: DynamicMethod and IL Generation

    I'm hoping someone here has some insight cuz I'm really stuck on this one

    So here's the scenario:

    1) I'm trying to dynamically create an event handler for a delegate type.

    2) I'd like for the event handler to simply call an anonymous method.

    I've been fooling around with DynamicMethod with some success as I'm able to get an instance of EventInfo, generate a simple DynamicMethod instance, and create a Delegate dynamically using the DynamicMethod.CreateDelegate(Type) method.

    While I haven't been able to figure out how to call the anonymous method that's passed in, I have been able to simply get it to write a line to the console using ILGenerator.EmitWriteLine(String) of the ILGenerator of the DynamicMethod.

    Where I've failed is the generation of the call to the anonymous method. For the most part, I'm convinced that my only error now seems to be in the IL generation after I've created the DynamicMethod instance.

    For testing purposes, I'm using the following simple code:

    Code:
    using System;
    using System.Reflection.Emit;
     
    namespace ConsoleApplication4 {
        internal class Program {
            public delegate void VoidNoArgs();
     
            private static void Main(string[] args) {
                Program program = new Program();
                program.Run();
            }
     
            private string text = "Hello World!";
     
            private void Run() {
                Helper helper = new Helper();
                helper.Test(delegate { Print(null); });
            }
     
            public void Print(string message) {
                Console.Out.WriteLine(message);
            }
        }
     
        internal class Helper {
            public void Test(Program.VoidNoArgs del) {
                // Test invocation of dynamic method
                DynamicMethod method = new DynamicMethod(
                    "NewMethod",
                    typeof (void),
                    new Type[] {},
                    typeof (Program));
     
                ILGenerator generator = method.GetILGenerator(256);
     
                generator.Emit(OpCodes.Ldarg_1);
                generator.Emit(OpCodes.Call, del.Method);
                generator.Emit(OpCodes.Ret);
     
                Delegate d = method.CreateDelegate(typeof (Program.VoidNoArgs));
     
                d.DynamicInvoke();
            }
        }
    }
    I've simplified it since I really only need to test the IL generation at this point.

    If I replace the code generation with:

    Code:
    generator.EmitWriteLine("Fixed string :(");
    generator.Emit(OpCodes.Ret);
    The code works fine, but obviously, it's only able to output a fixed string instead of having the desired effect of invoking the anonymous method.

    So, what am I doing wrong here

  2. #2
    Join Date
    May 2006
    Location
    East Brunswick, NJ, USA
    Posts
    27

    Default

    Wow, mere minutes after posting this (and messing with this for HOURS), I finally got it!

    Shoulda been:

    Code:
               generator.Emit(OpCodes.Ldnull);
               generator.Emit(OpCodes.Call, del.Method);
               generator.Emit(OpCodes.Ret);
    Hours!

    But now another issue...the call fails if I reference the local variable "text" in the anonymous method instead of using a string; seems like the newly created delegate can't access the local variables of the declaring class.
    Last edited by cdigs; 10-18-2007 at 03:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •