1

如果您使用相同的订阅多次订阅 .net 事件,那么您的订阅方法将被调用与订阅相同的时间。而且,如果您只取消订阅一次,那么通话只会减一分。这意味着您必须取消订阅的次数与订阅的次数相同,否则您将收到通知。有时你不想这样做。

为了防止一个事件处理程序被钩住两次,我们可以实现如下事件。

private EventHandler foo;
public event EventHandler Foo
{
    add
    {
        if( foo == null || !foo.GetInvocationList().Contains(value) )
        {
            foo += value;
        }
    }
    remove
    {
        foo -= value;
    }
}

现在我想实现 Postsharp EventInterceptionAspect 以使这个解决方案通用,这样我就可以应用于PreventEventHookedTwiceAttribute每个事件以节省大量代码。但我不知道如何在 add.xml 中检查以下条件的第二部分。我的意思是 foo.GetInvocationList().Contains(value)。我的 PreventEventHookedTwiceAttribute 如下所示。

[Serializable]
public class PreventEventHookedTwiceAttribute: EventInterceptionAspect
{
    public override void OnAddHandler(EventInterceptionArgs args)
    {
        if(args.Event == null || secondConditionRequired) // secondConditionRequired means it is required.            
        {
            args.ProceedAddHandler();
        }
    }
}

我不需要重写 OnRemoveHandler,因为这里的默认功能就足够了。

4

1 回答 1

2

这个类可以解决问题。

[Serializable]
public class PreventEventHookedTwiceAttribute: EventInterceptionAspect
{
    private readonly object _lockObject = new object();
    readonly List<Delegate> _delegates = new List<Delegate>();

    public override void OnAddHandler(EventInterceptionArgs args)
    {
        lock(_lockObject)
        {
            if(!_delegates.Contains(args.Handler))
            {
                _delegates.Add(args.Handler);
                args.ProceedAddHandler();
            }
        }
    }

    public override void OnRemoveHandler(EventInterceptionArgs args)
    {
        lock(_lockObject)
        {
            if(_delegates.Contains(args.Handler))
            {
                _delegates.Remove(args.Handler);
                args.ProceedRemoveHandler();
            }
        }
    }
}

显示差异的示例用法如下所示。

class Program
    {
        private static readonly object _lockObject = new object();
        private static int _counter = 1;

        [PreventEventHookedTwice]
        public static event Action<string> GoodEvent;


        public static event Action<string> BadEvent;

        public static void Handler (string message)
        {
            lock(_lockObject)
            {
                Console.WriteLine(_counter +": "+ message);
                _counter++;
            }
        }

        static void Main(string[] args)
        {
            GoodEvent += Handler;
            GoodEvent += Handler;
            GoodEvent += Handler;
            GoodEvent += Handler;
            GoodEvent += Handler;
            Console.WriteLine("Firing Good Event. Good Event is subscribed 5 times from the same Handler.");
            GoodEvent("Good Event is Invoked.");

            _counter = 1;
            BadEvent += Handler;
            BadEvent += Handler;
            BadEvent += Handler;
            BadEvent += Handler;
            BadEvent += Handler;
            Console.WriteLine("Firing Bad Event. Bad Event is subscribed 5 times from the same Handler.");
            BadEvent("Bad Event is Invoked.");

            _counter = 1;
            GoodEvent -= Handler;
            Console.WriteLine("GoodEvent is unsubscribed just once. Now fire the Event");
            if(GoodEvent!= null)
            {
                GoodEvent("Good Event Fired");
            }
            Console.WriteLine("Event is not received to Handler.");

            BadEvent -= Handler;
            Console.WriteLine("BadEvent is unsubscribed just once. Now fire the Event");
            BadEvent("Good Event Fired");
            Console.WriteLine("Event is fired 4 times. If u subscribe good event 5 times then u have to unscribe it for 5 times, otherwise u will be keep informed.");

            Console.ReadLine();
        }
    }

锋利的岩石。

于 2012-04-20T16:17:55.140 回答