samedi 15 juin 2019

Common method signature for methods with different parameters (avoid code duplication)

I have a concern, where I have to perform an action in the same manner, i.e. if method fails with certain error message, repeat it until specified number of times, similair to this:

public void PerformMethod(Func<string> method, int retrials)
{
  int trial = 1;
  string errMsg = method();

  while(errMsg == "some specific error" && trial < retrials)
  {
    errMsg = method();
    trial++;
  }
}

This looks pretty, but I need to perform different methods in this manner, let's say Func<int, string> and Func<int, DateTime, MyClass, string> (both return string as an error message to check).

I think there's simple solution to this, but yet I am unable to figure it out.

I thought about Func<object, string>, but it would require lot's of casting and also passing a Tuple in case of many arguments, which would become very ugly.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire