Exemple #1
0
        private bool GetDelegateInformationFromSyncMethod(string methodName, IDictionary <string, SyncEventMethodInfo> dictionary)
        {
            // First, try to get a delegate to the two parameter handler
            MethodInfo parameterfulMethod = GetInstanceMethodInfo(typeof(EventHandler), methodName);

            if (parameterfulMethod != null)
            {
                dictionary[methodName] = new SyncEventMethodInfo(parameterfulMethod, isArgless: false);
                return(true);
            }

            // If there isn't one, try the argless one
            MethodInfo parameterlessMethod = GetInstanceMethodInfo(typeof(VoidMethod), methodName);

            if (parameterlessMethod != null)
            {
                dictionary[methodName] = new SyncEventMethodInfo(parameterlessMethod, isArgless: true);
                return(true);
            }

            return(false);
        }
    private bool GetDelegateInformationFromSyncMethod(string methodName, IDictionary<string, SyncEventMethodInfo> dictionary) {
        // First, try to get a delegate to the two parameter handler
        MethodInfo parameterfulMethod = GetInstanceMethodInfo(typeof(EventHandler), methodName);
        if (parameterfulMethod != null) {
            dictionary[methodName] = new SyncEventMethodInfo(parameterfulMethod, isArgless: false);
            return true;
        }

        // If there isn't one, try the argless one
        MethodInfo parameterlessMethod = GetInstanceMethodInfo(typeof(VoidMethod), methodName);
        if (parameterlessMethod != null) {
            dictionary[methodName] = new SyncEventMethodInfo(parameterlessMethod, isArgless: true);
            return true;
        }

        return false;
    }
Exemple #3
0
        internal void HookUpAutomaticHandlers()
        {
            // Do nothing if auto-events are not supported
            if (!SupportAutoEvents)
            {
                return;
            }

            // Get the event list for this Type from our cache, if possible
            object    o = _eventListCache[GetType()];
            EventList eventList;

            // Try to find what handlers are implemented if not tried before
            if (o == null)
            {
                lock (_lockObject) {
                    // Try the cache again, in case another thread took care of it
                    o = (EventList)_eventListCache[GetType()];

                    if (o == null)
                    {
                        eventList = new EventList();

                        GetDelegateInformation(eventList);

                        // Cannot find any known handlers.
                        if (eventList.IsEmpty)
                        {
                            o = _emptyEventSingleton;
                        }
                        else
                        {
                            o = eventList;
                        }

                        // Cache it for next time
                        _eventListCache[GetType()] = o;
                    }
                }
            }

            // Don't do any thing if no known handlers are found.
            if (o == _emptyEventSingleton)
            {
                return;
            }

            eventList = (EventList)o;
            IDictionary <string, SyncEventMethodInfo> syncEvents = eventList.SyncEvents;

            // Hook up synchronous events
            foreach (var entry in syncEvents)
            {
                string key = entry.Key;
                SyncEventMethodInfo info = entry.Value;

                Debug.Assert(_eventObjects[key] != null);

                bool       eventExists = false;
                MethodInfo methodInfo  = info.MethodInfo;

                Delegate eventDelegates = Events[_eventObjects[key]];
                if (eventDelegates != null)
                {
                    foreach (Delegate eventDelegate in eventDelegates.GetInvocationList())
                    {
                        // Ignore if this method is already added to the events list.
                        if (eventDelegate.Method.Equals(methodInfo))
                        {
                            eventExists = true;
                            break;
                        }
                    }
                }

                if (!eventExists)
                {
                    // Create a new Calli delegate proxy
                    IntPtr       functionPtr = methodInfo.MethodHandle.GetFunctionPointer();
                    EventHandler handler     = (new CalliEventHandlerDelegateProxy(this, functionPtr, info.IsArgless)).Handler;

                    // Adds the delegate to events list.
                    Events.AddHandler(_eventObjects[key], handler);
                }
            }

            // Hook up asynchronous events
            IDictionary <string, AsyncEventMethodInfo> asyncEvents = eventList.AsyncEvents;

            AsyncEventMethodInfo preRenderCompleteAsyncEvent;

            if (asyncEvents.TryGetValue(_pagePreRenderCompleteAsyncEventName, out preRenderCompleteAsyncEvent))
            {
                Page page = (Page)this; // this event handler only exists for the Page type
                if (preRenderCompleteAsyncEvent.RequiresCancellationToken)
                {
                    var handler = FastDelegateCreator <Func <CancellationToken, Task> > .BindTo(this, preRenderCompleteAsyncEvent.MethodInfo);

                    page.RegisterAsyncTask(new PageAsyncTask(handler));
                }
                else
                {
                    var handler = FastDelegateCreator <Func <Task> > .BindTo(this, preRenderCompleteAsyncEvent.MethodInfo);

                    page.RegisterAsyncTask(new PageAsyncTask(handler));
                }
            }
        }