示例#1
0
        public WeakDelegate(T @delegate, Action <T> callback)
        {
            this.Callback = callback;
            var del = (Delegate)(object)@delegate;

            this.Type   = del.GetType();
            this.Method = del.Method;
            if (del.Target != null)
            {
                this.Reference       = new WeakReference(del.Target);
                this.WrapperDelegate = this.CreateDelegate();
            }

            WeakDelegateCollector.RegisterWeakDelegate(this);
            if (del.Target == null)
            {
                this.WrapperDelegate = WeakDelegateCollector.GetStaticDelegate(@delegate);
            }
        }
示例#2
0
        public bool CheckDelegate()
        {
            if (this.Reference.Target == null)
            {
                Action <T> callback = null;
                lock (this) {
                    callback      = this.Callback;
                    this.Callback = null;
                }

                if (callback != null)
                {
                    callback(this.WrapperDelegate);
                }

                WeakDelegateCollector.UnregisterWeakDelegate(this);
                return(false);
            }

            return(true);
        }