I have this below piece of code, which i always highlighted by SONAR as a MAJOR issue, because of violation of a rule called with the below message.
Multiple (3) calls to virtual property 'System.String System.Reflection.MemberInfo::get_Name()'.
And the rule description says
AvoidRepetitiveCallsToPropertiesRule gendarme : AvoidRepetitiveCallsToPropertiesRule The rule warn if virtual, or unlikely to be inline-able, property getters are called several times by a method. In most cases repetitive calls simply requires more time without any gains since the result will always be identical. You should ignore the reported defects if a different value is expected each time the property is 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 the SONAR is complaining about the line prop.Name which i have inside the foreach loop? How can i avoid it?
Aucun commentaire:
Enregistrer un commentaire