c# - Method To Set Nested Property Value -


i'm writing plugin piece of software requires code in single class (or nested classes)...

what i'm wanting create method can handle changes nested properties of _data object , give me central place things setting dirty flag know save later.

below code illustrating file's structure , @ bottom 2 pseudo-methods can give idea of i'm trying accomplish.

public class someplugin {     private dataobject _data;     private bool _dataisdirty = false;      private class dataobject     {         public generalsettings settings { get; set; }     }      private class generalsettings     {         public string settingone { get; set; }         public string settingtwo { get; set; }     }      protected override void init()     {         _data = new dataobject         {             settings = new generalsettings             {                 settingone = "example value one.",                 settingtwo = "example value two."             }         }     }      // these pseudo-methods illustrating i'm trying do.     private void setdata<t>(t ref instanceproperty, t newvalue)     {         if (newvalue == null) throw new argumentnullexception("newvalue");         if (instanceproperty == newvalue) return;          instanceproperty = newvalue;         _dataisdirty = true;     }      private void someothermethod()     {         setdata(_data.settings.settingone, "updated value one.");     }  } 

consider approach like:

public class someplugin {     private dataobject _data;     private bool _dataisdirty = false;      public bool isdirty => _dataisdirty || (_data?.isdirty ?? false);      private class dataobject     {         private bool _dataisdirty = false;          public bool isdirty => _dataisdirty || (settings?.isdirty ?? false);         public generalsettings settings { get; set; }     }      private class generalsettings     {         public bool isdirty { get; set; }          private string _settingone;          public string settingone         {             { return _settingone; }             set             {                 if (value != _settingone)                 {                     isdirty = true;                     _settingone = value;                 }             }         }          public string settingtwo { get; set; } // won't mark dirty     } } 

note in particular settingone has logic in setter determine whether set isdirty or not.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -