I have the following proof of concept classes:
private class SourceMock
{
public event PropertyChangedEventHandler TestEvent;
public void Raise()
{
if (this.TestEvent != null)
{
this.TestEvent(this, new PropertyChangedEventArgs("S"));
}
}
}
private class HandlerMock
{
public HandlerMock()
{
}
public void PropertyHandler(object sender, PropertyChangedEventArgs e)
{
}
}
Then if I do the following it works:
SourceMock sourceMock = new SourceMock();
HandlerMock handler = new HandlerMock();
sourceMock.GetType().GetEvent("TestEvent").AddEventHandler(sourceMock, new PropertyChangedEventHandler(handler1.PropertyHandler));
But if do the following it does not work:
SourceMock sourceMock = new SourceMock();
HandlerMock handler = new HandlerMock();
sourceMock.GetType().GetEvent("TestEvent").AddEventHandler(sourceMock, new EventHandler<PropertyChangedEventArgs>(handler1.PropertyHandler));
because I get the following exception:
System.ArgumentException: Object of type 'System.EventHandler`1[System.ComponentModel.PropertyChangedEventArgs]' cannot be converted to type 'System.ComponentModel.PropertyChangedEventHandler'.
I want to have a generic class for dealing with events and I cannot use delegates as generic types I have to use EventArgs which imposes me the second syntax (the non-working one).
Any ideas on how to overcome this?
Aucun commentaire:
Enregistrer un commentaire