c# - Why this call to AddEventHandler is not working? -


i have 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 following works:

    sourcemock sourcemock = new sourcemock();     handlermock handler = new handlermock();      sourcemock.gettype().getevent("testevent").addeventhandler(sourcemock, new propertychangedeventhandler(handler1.propertyhandler)); 

but if following not work:

    sourcemock sourcemock = new sourcemock();     handlermock handler = new handlermock();      sourcemock.gettype().getevent("testevent").addeventhandler(sourcemock, new eventhandler<propertychangedeventargs>(handler1.propertyhandler)); 

because following exception:

system.argumentexception: object of type 'system.eventhandler`1[system.componentmodel.propertychangedeventargs]' cannot converted type 'system.componentmodel.propertychangedeventhandler'.

i want have generic class dealing events , cannot use delegates generic types have use eventargs imposes me second syntax (the non-working one).

any ideas on how overcome this?

edit: i'm trying achieve

i need class intercept events , things before raising them (like counting them, filtering them, reordering them, etc) . clients not register events ask class register them event. signature thought following one:

public class eventhandlerservice<t> t : eventargs {     public void register(object eventsource, string eventname, eventhandler<t> handler) } 

when event happens service call registered handlers based on internal algorithm.

internally working eventinfo class , not working eventhandlers not derived eventhandler question.

delegate type event isn't necessary inherited eventhandler/eventhandler<t>, can see. so, maybe generics here superfluous:

public class eventhandlerservice {     /// <example>     /// use overload, when delegate type event isn't inherited eventhandler/eventhandler{t}.     /// <code>     /// service.register(someobj, "someevent", new propertychangedhandler(thehandler));     /// </code>     /// </example>     public void register(object eventsource, string eventname, delegate handler)     {         // work here     }      /// <example>     /// use overload, when delegate type event inherited eventhandler{t}.     /// <code>     /// service.register<fooeventargs>(someobj, "someevent", thehandler);     /// </code>     /// </example>     public void register<t>(object eventsource, string eventname, eventhandler<t> handler)         t : eventargs     {         register(eventsource, eventname, handler);     }      /// <example>     /// use overload, when delegate type event inherited eventhandler.     /// <code>     /// service.register(someobj, "someevent", thehandler);     /// </code>     /// </example>     public void register(object eventsource, string eventname, eventhandler handler)     {         // need cast here compiler find desired overload         register(eventsource, eventname, (delegate)handler);     } } 

particular problem question can solved way:

var eventinfo = sourcemock.gettype().getevent("testevent"); eventinfo.addeventhandler(sourcemock, delegate.createdelegate(eventinfo.eventhandlertype, handler, "propertyhandler")); 

but looks don't need solve - remove generics.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -