c# - INotifyPropertyChanged with lambda based checkin on the receiving end -


there's plenty of code , example floating around on how use lambda expressions raise inotifypropertychanged events refactoring freiendly code. typically used as:

notifyofpropertychanged(() => propertyname); 

my question is, how achieve similar on receiving end of things, you'd typcially have switch statement of sorts:

private void someobject_propertychanged(object sender, propertychangedeventargs args) {     switch (args.propertyname)     {         case "xyz":             break;         //...     } } 

now nice somehow avoid using strings here things not break when changing property name...

as half-way solution use embedded class public constants such as:

public class myclass {     public static class notifications     {         public const string property1 = "property1"         //...     }     //... } 

and on receiving end:

private void someobject_propertychanged(object sender, propertychangedeventargs args) {     switch (args.propertyname)     {         case myclass.notifictions.property1:             break;         //...     } } 

this better pure strings have maintained in 1 place , in same class make changes property names still not satisfying solution...

does know better way?

it depends on want property. assuming want value of changed property, can use reflection it:

private void someobject_propertychanged(object sender, propertychangedeventargs args) {     object value =          sender.gettype().getproperty(args.propertyname).getvalue(sender, null); } 

if needs not fulfilled reflection can still use string comparison , not worry property name changing using same mechanism you're using in notifypropertychanged method. if don't use one, can use mine (code here):

private void someobject_propertychanged(object sender, propertychangedeventargs args) {     if (args.propertyname == extendedobject.getpropertyname(_ => _.property1)     {         // ...     } } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -