CreateDelegate() public static method

Generates a delegate with a matching signature of the supplied eventHandlerType This method only supports Events that have a delegate of type void
public static CreateDelegate ( Type eventHandlerType, MethodInfo methodToInvoke, object methodInvoker ) : Delegate
eventHandlerType System.Type
methodToInvoke System.Reflection.MethodInfo The method to invoke
methodInvoker object The object where the method resides
return System.Delegate
Beispiel #1
0
        //Creates an EventHandler on runtime and registers that handler to the Event specified
        public void BindEvent(DependencyObject owner, string eventName)
        {
            EventName = eventName;
            Owner     = owner;
            Event     = Owner.GetType().GetEvent(EventName, BindingFlags.Public | BindingFlags.Instance);
            if (Event == null)
            {
                throw new InvalidOperationException(String.Format("Could not resolve event name {0}", EventName));
            }

            //Create an event handler for the event that will call the ExecuteCommand method
            EventHandler = EventHandlerGenerator.CreateDelegate(
                Event.EventHandlerType, typeof(CommandBehaviorBinding).GetMethod("ExecuteCommand", BindingFlags.Public | BindingFlags.Instance), this);
            //Register the handler to the Event
            Event.AddEventHandler(Owner, EventHandler);
        }
Beispiel #2
0
        /// <summary>
        /// Creates an EventHandler on runtime and registers that handler to the Event specified
        /// </summary>
        public void BindEvent(DependencyObject owner, string eventName)
        {
            if (string.IsNullOrEmpty(eventName))
            {
                return; // when unloading WPF styles, an 'eventName' of "" (empty string) can be set temporarily
            }
            EventName = eventName;
            Owner     = owner;
            Event     = Owner.GetType().GetEvent(EventName, BindingFlags.Public | BindingFlags.Instance);
            if (Event == null)
            {
                throw new InvalidOperationException(String.Format("Could not resolve event name {0}", EventName));
            }

            //Create an event handler for the event that will call the ExecuteCommand method
            EventHandler = EventHandlerGenerator.CreateDelegate(
                Event.EventHandlerType, typeof(CommandBehaviorBinding).GetMethod("Execute", BindingFlags.Public | BindingFlags.Instance), this);
            //Register the handler to the Event
            Event.AddEventHandler(Owner, EventHandler);
        }