I know we shouldn´t care if an eventhandler was registered for an event from outside that event, as seen here for instance How to determine if an event is already subscribed
Thus I have some code within the events-accessor to prevent any users of my API bothering about if or if not they already registered the handler:
private EventHandler foo;
public event EventHandler Foo
{
add
{
if (foo == null || !foo.GetInvocationList().Contains(value))
{
foo += value;
}
}
remove
{
foo -= value;
}
}
Now I want to write a test to verify that this check works correct:
[Test]
public void MyTest()
{
myInstance.Foo += DoSomething;
myInstance.Foo += DoSomething;
}
void DoSomething(object sender, EventArgs args) { ... }
But how do I check if foo
contains only a single handler, not two? Should I use reflection on foo
to get its invocation-list or is there any better way?