How to extract event method name!

Hi,

Does anyone know is it possible to get method name which is placed to eventhandler.

Like

Button.Click += new EventHandler("but_Click");

how do I get string "but_Click" in runtime


Answer this question

How to extract event method name!

  • Ganesh Babu

    These are not suitable for my purposes. Those expect that you have to write tester class, I'm creating Automated tester which generates tester class by using UI. That's  the reason why I have to get dynamically method name. 
  • Bill Blum

    Hi,

    I'm creating automated gui tester. That's why i have to load assembly dynamically and find out all all ui objects and their events. Last problem I have is to find out what method for example button1 calls on click event. Please ask more details if you need...

    Thank's

    Niko

  • PRMARJORAM

    Niko,

    you could simulate the click event by calling the "PerformClick()" method on the button.

    You can find a good review on techniques for UI test automation in the following articles:

    http://msdn.microsoft.com/msdnmag/issues/05/01/TestRun/ (for a reflection-based approach)

    http://msdn.microsoft.com/msdnmag/issues/05/09/TestRun/ (for a low-level UI automation)

    However the first article assumes that you know the name of the event handling method that is triggered by the Click event.

    Hope this helps,

    Andres.


  • Jose I. Merino

    Hi,

    U can get the event information using the EventInfo object of Reflection namespace. but u need to specify the event that u r looking for.

    Hope this will help you.

    BindingFlags bindflag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

    Type typebindflag = typeof(System.Windows.Forms.Button);

    EventInfo eventinfo = typebindflag.GetEvent("Click", bindflag);

    if (eventinfo != null)

    {

    Console.WriteLine("Looking for the Click event in the Button class with the specified BindingFlags.");

    Console.WriteLine(eventinfo.ToString());

    }

    else

    Console.WriteLine("The Click event is not available with the Button class.");

    Thanks,
    Kedar


  • Aggelos Petropoulos

    Sorry,

    It' would be ok, but I have to get via reflection etc, Outside of that form.
    I get button as Object then I should extract a method as string.




  • nmahesh567

    Try this,

    using System.Diagnostics;

    StackFrame sf=new StackFrame(true);

    string methodName=sf.GetMethod().ToString();

    I have used this for error handling(logging errors).

    Regards,

    Nishanth
    http://www.nish.somee.com

     


  • MQH

    Try the following:

    EventHandler myDelegate = new System.EventHandler(this.button1_Click);
    this.button1.Click += myDelegate;
    s
    tring method = myDelegate.GetInvocationList()[0].Method.Name;

    Andres.


  • jimmy_b

    It only gives me info about base event but it won't tell which method click calls.

    --Niko 

  • Nate Dogg28871

    Been asked before. See my solution in this other topic.


  • dumb82

    The simple (but frustrating) answer is that there's no way of doing this in general. An event doesn't have to simply store the subscribed delegates in a single field - they could be stored in a hashtable, for instance. I believe most of the built-in controls

    For simple events defined in your own classes, you could try getting a field with the same name as the event, and get the invocation list for that. That won't help you if someone's using custom add/remove though.

    Jon



  • derker

    Can you provide more information about what you are trying to do

    Andres.


  • How to extract event method name!