Declare it like that:
public static class TryCatchExtention
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static void WithTryCatch(this object theClass, Action theMethod)
{
try
{
theMethod.Invoke();
}
catch (Exception ex)
{
log.Error("Error : {0} " + ex.Message);
throw new ApplicationException("");
}
}
}
Usage:
var yourClass = new YourClass();
//Pass here the method to be wrapped with a `try-catch` block
yourClass.WithTryCatch(this.YourMethod);}
Or with params:
yourClass.WithTryCatch(()=> this.YourMethod(params...));}
Notice:
This extension method extends any class
object and only supports the Action
delegate which:
Encapsulates a method that has no parameters and does not return a value.
You can always support more overloads for more delegates signatures.