GetDelegate() static private method

static private GetDelegate ( Type dtype, IntPtr callable ) : Delegate
dtype System.Type
callable System.IntPtr
return System.Delegate
Esempio n. 1
0
        //====================================================================
        // Register a new Python object event handler with the event.
        //====================================================================

        internal bool AddEventHandler(IntPtr target, IntPtr handler)
        {
            Object obj = null;

            if (target != IntPtr.Zero)
            {
                CLRObject co = (CLRObject)ManagedType.GetManagedObject(target);
                obj = co.inst;
            }

            // Create a true delegate instance of the appropriate type to
            // wrap the Python handler. Note that wrapper delegate creation
            // always succeeds, though calling the wrapper may fail.

            Type     type = this.info.EventHandlerType;
            Delegate d    = DelegateManager.GetDelegate(type, handler);

            // Now register the handler in a mapping from instance to pairs
            // of (handler hash, delegate) so we can lookup to remove later.
            // All this is done lazily to avoid overhead until an event is
            // actually subscribed to by a Python event handler.

            if (reg == null)
            {
                reg = new Hashtable();
            }
            object    key  = (obj != null) ? obj : this.info.ReflectedType;
            ArrayList list = reg[key] as ArrayList;

            if (list == null)
            {
                list     = new ArrayList();
                reg[key] = list;
            }
            list.Add(new Handler(Runtime.PyObject_Hash(handler), d));

            // Note that AddEventHandler helper only works for public events,
            // so we have to get the underlying add method explicitly.

            object[]   args = { d };
            MethodInfo mi   = this.info.GetAddMethod(true);

            mi.Invoke(obj, BindingFlags.Default, null, args, null);

            return(true);
        }
Esempio n. 2
0
        public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
        {
            DelegateObject self = (DelegateObject)GetManagedObject(tp);

            if (Runtime.PyTuple_Size(args) != 1)
            {
                string message = "class takes exactly one argument";
                return(Exceptions.RaiseTypeError(message));
            }

            IntPtr method = Runtime.PyTuple_GetItem(args, 0);

            if (Runtime.PyCallable_Check(method) != 1)
            {
                return(Exceptions.RaiseTypeError("argument must be callable"));
            }

            Delegate d = DelegateManager.GetDelegate(self.type, method);

            return(CLRObject.GetInstHandle(d, self.pyHandle));
        }