• emit


      //+ 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; }

      

  • 相关阅读:
    POJ 1321:棋盘问题
    POJ 2251:Dungeon Master
    POJ 3438:Look and Say
    POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
    杭电1285--确定比赛名次(拓扑排序)
    南阳67--三角形面积
    南阳38--布线问题
    杭电1050--Moving Tables(区间覆盖)
    杭电1217--Arbitrage(Spfa)
    杭电1719--Friend(找规律)
  • 原文地址:https://www.cnblogs.com/hualiu0/p/4943240.html
Copyright © 2020-2023  润新知