c# - Adding Event to a Method -
i have http class i've created, , to, if possible, attach method each time either http.post() or http.get() called. i'd able attach event either before, or after.
i see being done in lot of frameworks web, such wordpress , symfony, i'd similar on winforms app.
what design pattern looking achieve can go , google it.
you can create events on objects , invoke them anywhere in code. quite often.
public event eventhandler getting; public event eventhandler setting; void get() { if (getting != null) getting.invoke(this, eventargs.empty); } void post() { if (setting != null) setting.invoke(this, eventargs.empty); }
then other classes can handle these events this:
myhttp.getting += myhttp_getting; public void myhttp_getting(object sender, eventargs e) { }
edits:
you invoke event this:
void post() { if (setting != null) setting(this, eventargs.empty); }
you can invoke event within own class. not problem because being able otherwise cause confusing code. check if null, because if there no subscribers null , through nullreferenceexception
when invoke it.
Comments
Post a Comment