mardi 10 décembre 2019

DelegateMethod to improve Reflection on set of Objects

I have a situation where I have a very large list of objects for which I want to call a user-defined list of methods (it's for building a generic report of the list of objects). The method signature is provided as MethodInfo (via reflection). Right now, I use method.Invoke to call this method for all of my individual objects to retrieve the individual values of my objects.

Something along those lines:

private void writeReport (List<object> rows, MethodInfo[] columns, string dest){
   System.IO.StreamWriter writer = new System.IO.StreamWriter(dest);

   StringBuilder builder = new StringBuilder();
   foreach(object row in rows){
      foreach(MethodInfo m in columns){
         builder.Append(m.Invoke(row).ToString());
      }
      writer.WriteLine(builder.ToString());
      builder.Clear();
   }

   writer.Close();
}

Note: This is just a dummy method to get an idea of my framework. There are obviously things missing like a header or separator signs etc.

I read about DelegateReflection and how much faster it is to the regular reflection Invoke call but the examples I found never have to call the same method on different objects. It's always a static method rather then a member method of an object.

Is there any other way how I can make this faster? Note that my list of "rows" can easily contain > 10'000'000 elements.

ALSO NOTE: I have seen this post MethodInfo.Invoke performance issue

However, I was wondering whether something better has come up since (also empirically trying this suggestion did not yield any performance gain).





Aucun commentaire:

Enregistrer un commentaire