Getting list of delegate function names appended to an event


#1

@stevebaer,

when i added my own behaviour to an event using eg. something like this:

scriptcontext.doc.ReplaceRhinoObject += MyDelegateFunctionName

is there a way to receive all delegate function names added to the event ? I know your example using scriptcontext.sticky to remember that an event has been added. But i would like to be independent of sticky, in case someone clears it…

c.


(Dale Fugier) #2

I don’t believe so. Perhaps if you own the event…


#3

@dale, thanks.I am not sure what that means and think you own it :wink:

The only info i find online about that is something like e.GetInvocationList(), but i am trying to get that list outside of the class using python.

c.


(Nathan 'jesterKing' Letwory) #4

You could try writing an extension method. I don’t know if you can in Python, but you could in C# write a small assembly that has extension methods for all the types you want to inspect. In this stackoverflow answer there is a sample for WPF controls:

    static Delegate[] GetEventHandlers(this Control ctrl, string eventName)
    {
            PropertyInfo propertyInfo = ctrl.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            EventHandlerList eventHandlerList = propertyInfo.GetValue(ctrl, new object[] { }) as EventHandlerList;
            FieldInfo fieldInfo = typeof(Control).GetField("Event"+eventName, BindingFlags.NonPublic | BindingFlags.Static);
 
           object eventKey = fieldInfo.GetValue(ctrl);
            var eventHandler = eventHandlerList[eventKey] as Delegate;
            Delegate[] invocationList = eventHandler.GetInvocationList();

            return invocationList;
    }

It then would be used like:

var handlers = someWpfCtrl.GetEventHandlers("GotFocus");


#5

@nathanletwory, thanks, i’ve seen that post. My problem is that i do not know about VS nor how to find all the imports needed to get this working from the python editor. Apart from this it would not run on all platforms which is required.

I guess i stick with the scriptcontext.sticky to save the event and try to “remember” it from there.

c.