c# - CallerMemberName in an extension (INotifyPropertyChanged) -


i implementing extension inotifiypropertychanged interface, can read this:

inotifypropertychanged - event stays null

for furhter information.

now extend extension further dont need state memberexpression , when calling inside set callermembername attribute rest.

so have tried following (based on links provided in last stackoverflow question):

public static void notify(this propertychangedeventhandler eventhandler, object sender,  [callermembername] string propertyname = "") {     if (eventhandler != null)     {         eventhandler(sender, new propertychangedeventargs(propertyname));     } } 

this allows me call method this:

this.propertychanged.notify(this); //with callermembername this.propertychanged.notify(this, "randomproperty");  

now remove neccessarity write (this, ..) parameter , call so:

this.propertychanged.notify(); //with callermembername this.propertychanged.notify("randomproperty");  

how possible?

simply, isn't possible. need 3 pieces of information:

  • the event-handler instance (this.propertychanged, on left)
  • the event-name (propertyname, supplied compiler)
  • the sender (sender)

sender cannot inferred of other information, , there no compiler option provide it. however, frankly, use instance method instead:

protected virtual void onpropertychanged([callermembername] string propertyname = null) {     var handler = propertychanged;     if(handler != null) handler(this, new propertychangedeventargs(propertyname)); } 

then caller issues:

onpropertychanged(); // job done 

you of course have onpropertychanged call static method, seems unnecessary.

in ways feels should able pass in inotifypropertychanged instance use both sender , access propertychanged, of course can't actual delegate event declaration.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -