You can use Actionand Func<T>to wrap methods and closures to pass arguments.
public TResult CallMethod<TResult>(Func<ThirdPartyClass, TResult> func)
{
try
{
return func(this.wrappedObject);
}
catch(ThirdPartyException e)
{
}
}
public void CallMethod(Action<ThirdPartyClass> method)
{
this.CallMethod(() => { method(this.WrappedObject); return 0; });
}
Then you can use this via:
var result = wrapper.CallMethod(thirdParty => thirdParty.Foo(bar, baz));
Edit: The assumption above was that you were completing an instance of a third-party library. Given (from your comments) that these are static methods, you can simply use:
public static TResult CallMethod<TResult>(Func<TResult> func)
{
try
{
return func();
}
catch(ThirdPartyException e)
{
}
}
public static void CallMethod(Action method)
{
CallMethod(() => { method(); return 0; });
}
And then call through:
var result = Wrapper.CallMethod(() => ThirdParty.Foo(bar, baz));
source
share