jeudi 7 décembre 2017

Performance dynamic vs Reflection

I have the following possibilities to retrieve values in a list:

using reflection:

foreach (var item in items) {
  var property=item.Fields[fieldName].GetType().GetProperty("Id");
  var value=property.GetValue(item.Fields[fieldName]);
  if (value==searchValue) {
      filtered.Add(item);
   }
}

using dynamic:

foreach (var item in items) {
   dynamic itemProperty=item.Fields[fieldName];
   if (itemProperty.Id==searchValue) {   
      filtered.Add(item);
   }
}

Both loops do the same. They filter the IEnumerable (or List) by Field[fieldName] which can be of different types but which all contain a int-property called "Id".

I wonder, which one would have a better performance. In addition: Would changing one of those to a LinQ-Query increase the performance?





Aucun commentaire:

Enregistrer un commentaire