GetDelegate() public method

Creates a delegate with a given signature that could be used to invoke this object from non-dynamic code (w/o code context). A stub is created that makes appropriate conversions/boxing and calls the object. The stub should be executed within a context of this object's language.
The object is either a subclass of Delegate but not the requested type or does not implement IDynamicMetaObjectProvider.
public GetDelegate ( object callableObject, Type delegateType ) : Delegate
callableObject object
delegateType System.Type
return System.Delegate
Exemplo n.º 1
0
        public void AddHandler(object target, object handler, DynamicDelegateCreator delegateCreator) {
            ContractUtils.RequiresNotNull(handler, "handler");
            ContractUtils.RequiresNotNull(delegateCreator, "delegateCreator");

            Delegate delegateHandler;
            HandlerList stubs;

            // we can add event directly (signature does match):
            if (_eventInfo.EventHandlerType.IsAssignableFrom(handler.GetType())) {
                delegateHandler = (Delegate)handler;
                stubs = null;
            } else {
                // create signature converting stub:
                delegateHandler = delegateCreator.GetDelegate(handler, _eventInfo.EventHandlerType);
                stubs = GetHandlerList(target);
            }

            var add = GetCallableAddMethod();

            // TODO (tomat): this used to use event.ReflectedType, is it still correct?
            if (target != null) {
                add = CompilerHelpers.TryGetCallableMethod(target.GetType(), add);
            }

            add.Invoke(target, new object[] { delegateHandler });

            if (stubs != null) {
                // remember the stub so that we could search for it on removal:
                stubs.AddHandler(handler, delegateHandler);
            }
        }
Exemplo n.º 2
0
        public void AddHandler(object target, object handler, DynamicDelegateCreator delegateCreator) {
            ContractUtils.RequiresNotNull(handler, "handler");
            ContractUtils.RequiresNotNull(delegateCreator, "delegateCreator");

            Delegate delegateHandler;
            HandlerList stubs;

            // we can add event directly (signature does match):
            if (_eventInfo.EventHandlerType.IsAssignableFrom(handler.GetType())) {
                delegateHandler = (Delegate)handler;
                stubs = null;
            } else {
                // create signature converting stub:
                delegateHandler = delegateCreator.GetDelegate(handler, _eventInfo.EventHandlerType);
                stubs = GetHandlerList(target);
            }

            GetCallableAddMethod().Invoke(target, new object[] { delegateHandler });

            if (stubs != null) {
                // remember the stub so that we could search for it on removal:
                stubs.AddHandler(handler, delegateHandler);
            }
        }