当前位置: 移动技术网 > IT编程>开发语言>c# > .NET(C#):Emit创建异常处理的方法

.NET(C#):Emit创建异常处理的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
目录 emit异常处理流程 显示exception对象的message属性 返回目录 emit异常处理流程来看这种c#异常处理代码: 复制代码 代码如下: &

目录

emit异常处理流程
显示exception对象的message属性
返回目录
emit异常处理流程
来看这种c#异常处理代码:

复制代码 代码如下:

        static void doo(exception e)

        {

            try

            {

                throw e;

            }

            catch (applicationexception ex)

            {

                console.writeline("捕获applicationexception");

            }

            catch

            {

                console.writeline("捕获exception");

            }

            finally

            {

                console.writeline("finally块");

            }

        }

我们将用反射emit创建一个这样的方法。

其实il中的异常处理代码还是比较复杂的,你可以在reflector下看看异常处理的il代码。不过好在ilgenerator类提供了一些方便的方法来创建异常处理代码。

基本套路就是用如下ilgenerator的方法:

beginexceptionblock方法来开始异常处理代码(相当于try)。
之后的代码可以用opcodes.throw来抛出异常,或者调用其他可以抛出异常的代码。
接着用begincatchblock方法来开始一个catch块,该方法可以指定catch需要捕获的异常类型,另外有一点需要注意的是凡是进入该catch方法,逻辑栈上会有相应类型的异常对象。 同时,这里也可以用opcodes.rethrow来重新抛出异常。
最后beginfinallyblock方法开始一个finally块。 (这里不需要手动加opcodes.leave)
当全部异常处理代码写完后,加上endexceptionblock方法来结束整块异常处理代码块。
注意方法最后还是必须要加il的ret指令的(opcodes.ret),否则clr无法运行此方法。

来看代码:

复制代码 代码如下:

        //+ 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块


返回目录
显示exception对象的message属性
上面的代码并没有显示exception对象的message属性,上面主要是介绍emit异常处理的流程,下面来看看怎样显示message属性,如果是直接输出当然简单了,不过如果用到console.writeline的格式字符串的话,需要在catch代码块中用一个临时变量。

如下代码:

复制代码 代码如下:

        //+ using system.reflection;

        //+ using system.reflection.emit;
        static void main(string[] args)
        {
            var dm = getmethod();

            dm.invoke(null, new object[] { new exception("来自mgen!") });
        }

        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(exception));

            //临时变量 和 需要的反射信息

            var exp = ilgen.declarelocal(typeof(exception));

            var msg = typeof(exception).getproperty("message").getgetmethod();

            var output = typeof(console).getmethod("writeline", new type[] { typeof(string), typeof(object) });

            //保存异常对象到临时变量exp

            ilgen.emit(opcodes.stloc, exp);

            //格式字符串进栈

            ilgen.emit(opcodes.ldstr, "错误信息: {0}");

            //加载临时变量

            ilgen.emit(opcodes.ldloc, exp);

            //获取message属性

            ilgen.emit(opcodes.callvirt, msg);

            //调用有格式字符串的console.writeline

            ilgen.emit(opcodes.call, output);

            //结束整个处理块

            ilgen.endexceptionblock();

            ilgen.emit(opcodes.ret);

            return dm;

        }


输出:

复制代码 代码如下:

错误信息: 来自mgen!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网