c# - Changing the functionality of get;set; keywords -


in order avoid coding implemented dictionary store properties values:

 public class mainviewmodel {     public list<person> people { get; set; }     public person boss { get; set; }     int = -1;      public mainviewmodel()     {         boss = new person() { name = "the boss" };         people = new list<person>();          while (++i < 10)         {             people.add(new person() { name = $"person {i}" });         }          update();     }      private async void update()     {         await task.delay(1000);          boss.name = $"the boss {++i}";          update();     } }  public class person : model {     public string name     {         { return getproperty<string>(); }         set { setproperty(value); }     } }   public class model : inotifypropertychanged {     private dictionary<string, object> properties;     public event propertychangedeventhandler propertychanged;      public model()     {         properties = new dictionary<string, object>();     }      protected t getproperty<t>([callermembername] string key = null)     {         if(properties.containskey(key))         {             return (t)properties[key];         }         return default(t);     }      protected void setproperty<t>(t newvalue, [callermembername] string key = null)     {         properties[key] = newvalue;         notifypropertychanged(key);     }      public void notifypropertychanged([callermembername] string caller = "")     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(caller));         }     }  } 

as see if functionality of , set in person model use:

public string name { get;set;} 

would great simplify code. possible in someway? might in c# 7?

plenty of options reducing boilerplate code have been given you. if insist on solving problem changing how automatic getters , setters work, you're not going able that.

if understand default get;set; stores value somewhere, why cannot manage myself?

so write own custom getters , setters , use own custom backing field. that's they're there for. again, other options given there reduce boilerplate code, @ end of day, still have implement inotifypropertychanged using custom accessors , backing field, because additional logic falls outside scope of auto-implemented property.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -