Skip to content

Instantly share code, notes, and snippets.

@The-Quill
Created January 6, 2016 01:21
Show Gist options
  • Save The-Quill/6787ead18e0c6bd66e12 to your computer and use it in GitHub Desktop.
Save The-Quill/6787ead18e0c6bd66e12 to your computer and use it in GitHub Desktop.
using System;
namespace Test1
{
public class Utils
{
public static object InvokeAndCatchError(Delegate del, object parameters, params object[] optionalParameters)
{
try
{
return del.Method.Invoke(parameters, optionalParameters);
}
catch (Exception e)
{
Console.WriteLine("Exception occured:");
Console.WriteLine(e);
return null;
}
}
public static string Foo(string bar)
{
return bar + bar;
}
public delegate object Del(string bar);
public Utils(string lorem)
{
Del handler = Foo;
InvokeAndCatchError(handler, lorem); //Returns TargetException
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment