public static void TraceMethodAndDelegate(object sender, System.Delegate eventHandler) { if (!_enabled) return; try { StackFrame f = new StackFrame(1); Delegate[] eventHandlers = (eventHandler != null ? eventHandler.GetInvocationList() : new Delegate[] {}); // string target = (eventHandler != null ? eventHandlers[0].Target.GetType().Name : < string message = string.Format("{0}.{1} called, preparing to call {3} delegates.", f.GetMethod().DeclaringType.Name, f.GetMethod().Name, DateTime.Now.ToLongTimeString(), eventHandlers.Length.ToString()); System.Diagnostics.Trace.WriteLine(message); // eventHandler.Target.GetType().Name } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); } }
static void DelegateReport(System.Delegate d, string name) { if (d == null) return; IFormatProvider fp = System.Globalization.CultureInfo.InvariantCulture; string title = string.Format(fp, "{0} Event\n", name); UnsafeNativeMethods.CRhinoEventWatcher_LogState(title); Delegate[] list = d.GetInvocationList(); if (list != null && list.Length > 0) { for (int i = 0; i < list.Length; i++) { Delegate subD = list[i]; Type t = subD.Target.GetType(); string msg = string.Format(fp, "- Plug-In = {0}\n", t.Assembly.GetName().Name); UnsafeNativeMethods.CRhinoEventWatcher_LogState(msg); } } }
public static bool HasDelegate(System.Delegate mainDelegate, System.Delegate targetListener) { if (mainDelegate != null) { if (mainDelegate.GetInvocationList().Contains(targetListener)) { return true; } else { return false; } } else { return false; } }
private static void DisplayDelegateInfo(System.Delegate delObj) { foreach (System.Delegate d in delObj.GetInvocationList()) { Console.WriteLine("Method Name: {0}", d.Method); // is used only only when the delegate points to a non-static method in order to have a reference. // for example for SimpleMath.SubtractWithError we have a target reference to SimpleMath (otherwise it would be impossible to access to a member variable) Console.WriteLine("Type Name: {0}", d.Target); Console.WriteLine(string.Join(", " , d.GetMethodInfo().GetParameters().Select(x => string.Format("{0} {1}", x.Name, x.ParameterType)))); } }