public void AddAndRemoveEventHandlerThroughInterceptor()
{
TestObject target = new TestObject();
NopInterceptor nopInterceptor = new NopInterceptor();
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
target.Name = "SOME-NAME";
ProxyFactory pf = new ProxyFactory(target);
pf.AddAdvice(nopInterceptor);
pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
object proxy = pf.GetProxy();
ITestObject to = proxy as ITestObject;
// add event handler through proxy
to.Click += new EventHandler(OnClick);
OnClickWasCalled = false;
to.OnClick();
Assert.IsTrue(OnClickWasCalled);
Assert.AreEqual(2, countingBeforeAdvice.GetCalls());
// remove event handler through proxy
to.Click -= new EventHandler(OnClick);
OnClickWasCalled = false;
to.OnClick();
Assert.IsFalse(OnClickWasCalled);
Assert.AreEqual(4, countingBeforeAdvice.GetCalls());
}