//+ using System.Reflection;
//+ using System.Reflection.Emit;
static void Main(string[] args)
{
var dm = GetMethod();
dm.Invoke(null, new object[] { new ApplicationException() });
dm.Invoke(null, new object[] { new Exception() });
}
static DynamicMethod GetMethod()
{
var dm = new DynamicMethod("", null, new Type[] { typeof(Exception) });
var ilgen = dm.GetILGenerator();
//try {
ilgen.BeginExceptionBlock();
//加载第一个参数,并throw
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Throw);
ilgen.BeginCatchBlock(typeof(ApplicationException));
//清空栈上的异常对象
ilgen.Emit(OpCodes.Pop);
ilgen.EmitWriteLine("捕获ApplicationException");
ilgen.BeginCatchBlock(typeof(Exception));
//清空栈上的异常对象
ilgen.Emit(OpCodes.Pop);
ilgen.EmitWriteLine("捕获Exception");
ilgen.BeginFinallyBlock();
ilgen.EmitWriteLine("finally块");
//结束整个处理块
ilgen.EndExceptionBlock();
ilgen.Emit(OpCodes.Ret);
return dm;
}
输出:
复制代码 代码如下:
捕获ApplicationException
finally块
捕获Exception
finally块
//+ using System.Reflection; //+ using System.Reflection.Emit; static void Main(string[] args) { //创建DynamicMethod对象 var dm = GetFor(); //测试 dm.Invoke(null, new object[] { 3 }); } static DynamicMethod GetFor() { var dm = new DynamicMethod("", null, new Type[] { typeof(int) }); var gen = dm.GetILGenerator(); //临时变量i LocalBuilder locI = gen.DeclareLocal(typeof(int)); Label lbCondition = gen.DefineLabel(); Label lbTrue = gen.DefineLabel(); //i=0 gen.Emit(OpCodes.Ldc_I4_0); gen.Emit(OpCodes.Stloc, locI); //跳至判断 gen.Emit(OpCodes.Br, lbCondition); //标记True代码 gen.MarkLabel(lbTrue); //Console.WriteLine(i) gen.Emit(OpCodes.Ldloc, locI); gen.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) })); //追加代码 //i++ gen.Emit(OpCodes.Ldloc, locI); gen.Emit(OpCodes.Ldc_I4_1); gen.Emit(OpCodes.Add); gen.Emit(OpCodes.Stloc, locI); //判断代码 gen.MarkLabel(lbCondition); gen.Emit(OpCodes.Ldloc, locI); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Clt); //如果True,跳至true代码 gen.Emit(OpCodes.Brtrue, lbTrue); gen.Emit(OpCodes.Ret); return dm; }
static void doo(Exception e) { try { throw e; } catch (ApplicationException ex) { Console.WriteLine("捕获ApplicationException"); } catch { Console.WriteLine("捕获Exception"); } finally { Console.WriteLine("finally块"); } }
//+ using System.Reflection; //+ using System.Reflection.Emit; static void Main(string[] args) { var dm = GetMethod(); dm.Invoke(null, new object[] { new ApplicationException() }); dm.Invoke(null, new object[] { new Exception() }); } static DynamicMethod GetMethod() { var dm = new DynamicMethod("", null, new Type[] { typeof(Exception) }); var ilgen = dm.GetILGenerator(); //try { ilgen.BeginExceptionBlock(); //加载第一个参数,并throw ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Throw); ilgen.BeginCatchBlock(typeof(ApplicationException)); //清空栈上的异常对象 ilgen.Emit(OpCodes.Pop); ilgen.EmitWriteLine("捕获ApplicationException"); ilgen.BeginCatchBlock(typeof(Exception)); //清空栈上的异常对象 ilgen.Emit(OpCodes.Pop); ilgen.EmitWriteLine("捕获Exception"); ilgen.BeginFinallyBlock(); ilgen.EmitWriteLine("finally块"); //结束整个处理块 ilgen.EndExceptionBlock(); ilgen.Emit(OpCodes.Ret); return dm; }
原文:http://www.cnblogs.com/hualiu0/p/4943240.html