I want to create a simple one-line try/catch without all the extra fluff:
// The extension class
public static class TryExFunc
{
public static Exception TryEx<TResult> (this Func<TResult> func,
out TResult result)
{
Exception error = null;
try
{
result = func();
}
catch(Exception ex)
{
error = ex;
result = default(TResult);
}
return error;
}
}
// My Error Prone Function
public string SayHello() { throw new Exception(); }
// My Code
// One (ok, two) line(s) to try/catch a function call... ew, works, but ew
string result;
Exception error = ((Func<string>)SayHello).TryEx<string>(out result);
// I want to do this!!!
string result;
Exception error = SayHello.TryEx<string>(out result);
Is there a way that I can do the bottom example? I'm still learning C# (coming from Lua and C++ background). Lua has a really nice function called 'pcall' that basically does the same thing. Thanks for any advice or suggestions you have!
:)