using System.Runtime.CompilerServices;
using System.Reflection.Emit;
using static System.Linq.Expressions.Expression;
[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]
public static void TryAndCatch(Action tryBlock, params Delegate[] catchBlocks)
Lambda<Action>(TryCatch(Invoke(Constant(tryBlock)), catchBlocks.Select(i =>
var param = Parameter(i.Method.GetParameters()[0].ParameterType);
return Catch(param, Invoke(Constant(i), param));
}).ToArray())).Compile()();
public static void Main()
var dm = new DynamicMethod("ThrowObject", typeof (void), new [] { typeof (object) });
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
var throwObject = (Action<object>)dm.CreateDelegate(typeof (Action<object>));
(Action<string>)delegate (string o)
Console.WriteLine("String {0}", o);
(Action<DateTime>)delegate (DateTime o)
Console.WriteLine("DateTime {0}", o);
(Action<object>)delegate (object o)
Console.WriteLine("Object {0}", o);