Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Event/Delegate Emit and Reflection

Event/Delegate Emit and Reflection

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eric Astor
    wrote on last edited by
    #1

    I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the EventInfo object that represents the event. This includes a reference to the type (subclassed from Delegate and accessed through the EventInfo.EventHandlerType property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handler Type, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric Astor

    M L 2 Replies Last reply
    0
    • E Eric Astor

      I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the EventInfo object that represents the event. This includes a reference to the type (subclassed from Delegate and accessed through the EventInfo.EventHandlerType property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handler Type, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric Astor

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      This might get you on the right track:

      Assembly assembly.LoadFrom("my_assembly.dll");
      Type type=assembly.GetType("my_class");
      MethodInfo mi=type.GetMethod("my_method");

      Using MethodInfo, you can inspect the security, return type, parameters, etc. Lots of fun. [edit]Oops--LoadFrom, not GetType[/edit] Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
      Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
      Every line of code is a liability - Taka Muraoka
      Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

      E 1 Reply Last reply
      0
      • M Marc Clifton

        This might get you on the right track:

        Assembly assembly.LoadFrom("my_assembly.dll");
        Type type=assembly.GetType("my_class");
        MethodInfo mi=type.GetMethod("my_method");

        Using MethodInfo, you can inspect the security, return type, parameters, etc. Lots of fun. [edit]Oops--LoadFrom, not GetType[/edit] Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
        Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
        Every line of code is a liability - Taka Muraoka
        Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

        E Offline
        E Offline
        Eric Astor
        wrote on last edited by
        #3

        I already understand this much of Reflection, but that doesn't help me... I need to dynamically create a method without ever actually seeing any method, only an EventInfo object or something that it publicly exposes a reference to.

        M 1 Reply Last reply
        0
        • E Eric Astor

          I already understand this much of Reflection, but that doesn't help me... I need to dynamically create a method without ever actually seeing any method, only an EventInfo object or something that it publicly exposes a reference to.

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          I'm not really sure if this will help, but I use this to dynamically create a delegate:

          public delegate object InterfacePoint(EventData eventData);
          ...
          void RegisterInterfacePointByReflection(object instance, string fncName)
          {
          Type ipType=typeof(InterfacePoint);
          InterfacePoint ip=null;
          ip=Delegate.CreateDelegate(ipType, instance, fncName) as InterfacePoint;
          }

          You have to have an instance of the object containing the event sink though. If you don't have an instance, you have to pass CreateDelegate a MethodInfo instead. Is THIS anything closer to what you need? If not, can you give me an example of what you have and where you want to go with it? (Gee, that sounds a lot like a Microsoft tag line). Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
          Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
          Every line of code is a liability - Taka Muraoka
          Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

          E 1 Reply Last reply
          0
          • M Marc Clifton

            I'm not really sure if this will help, but I use this to dynamically create a delegate:

            public delegate object InterfacePoint(EventData eventData);
            ...
            void RegisterInterfacePointByReflection(object instance, string fncName)
            {
            Type ipType=typeof(InterfacePoint);
            InterfacePoint ip=null;
            ip=Delegate.CreateDelegate(ipType, instance, fncName) as InterfacePoint;
            }

            You have to have an instance of the object containing the event sink though. If you don't have an instance, you have to pass CreateDelegate a MethodInfo instead. Is THIS anything closer to what you need? If not, can you give me an example of what you have and where you want to go with it? (Gee, that sounds a lot like a Microsoft tag line). Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
            Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
            Every line of code is a liability - Taka Muraoka
            Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

            E Offline
            E Offline
            Eric Astor
            wrote on last edited by
            #5

            Alright... What I'm working on involves the dynamic creation of a wrapper class (through use of the Emit namespace) around a provided class that I see only as its Type object representation. As part of building the wrapper class, I need to emit methods for each event of the original class such that each method fits the requirements to be used in the delegate type used for the targeted event. Unfortunately, I can't seem to find any way to pull out anything that I could use to dynamically discover the required method signature for the creation of a method that can be used in the initialization of a provided delegate. The primary thing available to me with respect to the event is the EventInfo object, but the only thing that that exposes as to the event handler required for it is the Type object representing the class that inherits the System.Delegate class. This is a Type object like any other, and I can't find any way to pull the method signature out of it that it wants for its instantiation.

            1 Reply Last reply
            0
            • E Eric Astor

              I'm working on the basis for a CodeProject article, but stuck on a couple of issues. The current problem is as follows: I have to dynamically create a method that can be used as an event handler for a particular event, given only the EventInfo object that represents the event. This includes a reference to the type (subclassed from Delegate and accessed through the EventInfo.EventHandlerType property) that is created to represent the method in delegate form. However, there doesn't seem to be an obvious way to read out the desired method form (parameters & parameters' types, return type, etc.). Does anyone know how to get this method semi-signature out of the EventInfo, the event handler Type, or anything else that's dynamically accessible through the framework (almost certainly through Reflection)? Thanks! Eric Astor

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              I think this is what you are looking for :)

              public delegate void SymEventHandler(Symantic type, ParserContext context)
              {
              public SymEventHandler(object object, IntPtr method);
              public virtual IAsyncResult BeginInvoke(Symantic type, ParserContext context, AsyncCallback callback, object object);
              public virtual void EndInvoke(IAsyncResult result);
              public override void Invoke(Symantic type, ParserContext context);
              }

              Lookup the Invoke method with Reflection.

              leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              E 1 Reply Last reply
              0
              • L leppie

                I think this is what you are looking for :)

                public delegate void SymEventHandler(Symantic type, ParserContext context)
                {
                public SymEventHandler(object object, IntPtr method);
                public virtual IAsyncResult BeginInvoke(Symantic type, ParserContext context, AsyncCallback callback, object object);
                public virtual void EndInvoke(IAsyncResult result);
                public override void Invoke(Symantic type, ParserContext context);
                }

                Lookup the Invoke method with Reflection.

                leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                E Offline
                E Offline
                Eric Astor
                wrote on last edited by
                #7

                Looking up the Invoke method with Reflection is exactly what I needed... I really appreciate your help.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups