c# - What does SONAR Error AvoidRepetitiveCallsToPropertiesRule mean? -
i have below piece of code, highlighted sonar major issue, because of violation of rule called below message.
multiple (3) calls virtual property 'system.string system.reflection.memberinfo::get_name()'.
and rule description says
avoidrepetitivecallstopropertiesrule
gendarme : avoidrepetitivecallstopropertiesrule rule warn if virtual, or unlikely inline-able, property getters called several times method. in cases repetitive calls requires more time without gains since result identical. should ignore reported defects if different value expected each time property called (e.g. calling datetime.now).**
private static void overrideconfigurationvalues(configa configa, configb configb, configc configc) { type t = configa(); var properties = t.getproperties(bindingflags.public | bindingflags.instance); var overriddenvalues = new dictionary<string, object>(); foreach (var prop in properties) { var value = prop.getvalue(configa,null); if (value != null) { overriddenvalues.add(prop.name, value); } } type b = configb.gettype(); foreach (var prop in b.getproperties(bindingflags.public | bindingflags.instance)) { if (!overriddenvalues.containskey(prop.name)) { var value = prop.getvalue(b,null); if (value != null) { overriddenvalues.add(prop.name, value); } } } foreach (var overriddenvalue in overriddenvalues) { var overriden = overriddenvalue; foreach (var prop in configa.gettype().getproperties().where(prop => prop.name == overriden.key)) { prop.setvalue(configa, overriddenvalue.value,null); } } } if sonar complaining line prop.name have inside foreach loop? how can avoid it?
ron beyer's comments proper answers question.
so, code based on comments:
... foreach (var prop in b.getproperties(bindingflags.public | bindingflags.instance)) { var propname = prop.name; if (!overriddenvalues.containskey(propname)) { var value = prop.getvalue(b,null); if (value != null) { overriddenvalues.add(propname, value); } } } ... note support of gendarme rules dropped in 3.0 version of c# plugin.
Comments
Post a Comment