C#我可以使用反射来检查的方法code? [英] c# Can I use reflection to inspect the code in a method?

查看:112
本文介绍了C#我可以使用反射来检查的方法code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩周围用C#反射API。我可以很容易地加载的类,方法等类型的信息在装配中。

i'm playing around with c# reflection api. i can easily load type info of classes, methods etc. in an assembly.

现在我不知道如何可以加载并读取code的方法里?

now i wonder how can i load and read the code inside a method?

推荐答案

基本答案:

您无法使用反射API(的System.Reflection)。

You can't with the reflection API (System.Reflection).

原因在于反射API被设计为在元数据(类,名称和方法的签名类型,...),但无法在数据层面(这将是IL-流本身)工作。

The reason is that the reflection api is designed to work on Metadata (Type of Classes, Name and Signature of Methods, ...) but not on the data level (which would be the IL-stream itself).

扩展答:

您可以发射(但不读)IL与System.Reflection.Emit(例如的ILGenerator类)。

You can emit (but not read) IL with System.Reflection.Emit (e.g. ILGenerator Class).

MethodInfo.GetMethodBody()就可以得到二进制IL-流的方法的实现。但是那通常是由自己完全无用的。

Through MethodInfo.GetMethodBody() you can get the binary IL-stream for the implementation of a method. But thats usually completely useless by itself.

有,你可以用它来读取/修改/添加/删除$ C $外部库(如塞西尔) c进行方法内。

There are external libraries (like Cecil) that you can use to read/modify/add/delete code inside a method.

这篇关于C#我可以使用反射来检查的方法code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆