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 ThrowObj<T>(T obj)
Lambda<Action>(Throw(Constant(obj))).Compile()();
public static void Main()
(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);