c# - Get name and value of static class properties using Expression Trees -


i have generic method compares 2 properties, if value different logs changes , saves.

    private void saveifchanged<t>(expression<func<t>> expression, t newvalue)             {                  var expr = (memberexpression)expression.body;                  var obj = (memberexpression)expr.expression;                  var fieldsofobj = (constantexpression)obj.expression;                 var valuesofallfieldsofobj = ((fieldinfo)obj.member).getvalue(fieldsofobj.value);                   var propertyinfo = ((propertyinfo)expr.member);                  var oldpropertyvalue = propertyinfo.getvalue(valuesofallfieldsofobj, null);                   if (oldpropertyvalue.equals(newvalue)) return;           var desctiptionattributes = (descriptionattribute[])propertyinfo.getcustomattributes(typeof(descriptionattribute), false);                 log("{0} changed {1} {2}",desctiptionattributes[0].description, oldpropertyvalue, newvalue);                 propertyinfo.setvalue(valuesofallfieldsofobj, newvalue, null);                  save();             } 

it works fine when pass properties members of non-static class, when pass static property doesn't work.

saveifchanged(() => _settings.domainname, domainname); // works saveifchanged(() => settings.domainname, domainname);  //doesn't work 

i know how fields/properties of static class, when have class name. don't know how can combine following method.

        type s= typeof(settings);         fieldinfo[] fields = t.getfields(bindingflags.static | bindingflags.public);          foreach (fieldinfo fi in fields)         {             console.writeline(fi.name);             console.writeline(fi.getvalue(null).tostring());         } 

thank you.

the problem when try access property expression in var fieldsofobj = (constantexpression)obj.expression;.

memberexpression composed of 2 properties:

  • expression gets containing object of field or property.
  • member gets field or property accessed.

in first case (_settings.domainname) property expression gets memberexpression object containing _settings, , property member returns memberinfo pointing domainname.

in second case (settings.domainname), property expression returns null because not accessing property of instance, accessing static property. in code object obj null, , that's when problem comes.

you can see this question details.

to solve issue, can this:

private static void saveifchanged<t>(expression<func<t>> expression, t newvalue) {     var expr = (memberexpression)expression.body;      object valuesofallfieldsofobj = null;     if (expr.expression != null)     {         var obj = (memberexpression)expr.expression;         var fieldsofobj = (constantexpression)obj.expression;         valuesofallfieldsofobj = ((fieldinfo)obj.member).getvalue(fieldsofobj.value);     }      var propertyinfo = ((propertyinfo)expr.member);     var oldpropertyvalue = propertyinfo.getvalue(valuesofallfieldsofobj, null);      if (oldpropertyvalue.equals(newvalue)) return;      var desctiptionattributes = (descriptionattribute[])propertyinfo.getcustomattributes(typeof(descriptionattribute), false);     log("{0} changed {1} {2}", desctiptionattributes[0].description, oldpropertyvalue, newvalue);     propertyinfo.setvalue(valuesofallfieldsofobj, newvalue, null);      save(); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -