获取方法主体作为文本 [英] Get Method Body as Text

查看:47
本文介绍了获取方法主体作为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将方法主体检索为纯文本?
例如,如果我有如下方法:

Is there any way to retrieve the method body as pure text ?
For example if i have a method like the following one:

private void Test()
      {
          Response.Write("Hello");
      }



我想使用反射来检索零件Response.Write("Hello");
我可以获取有关方法的所有信息,例如类,参数以及除正文之外的其他所有内容,我

.net中的反射类可以将您的主体显示为字节数组[]


实际上,它是运行中的应用程序的外部程序集
所以我实际上是从程序集对象加载程序集,然后开始获取其下的信息的操作

关于这样做的可能性?我认为我们可能会导致许多反射应用程序为您提供dll内的所有内容,包括名称空间-类-方法(与源代码中所写的完全相同)

有什么帮助吗?



I want using reflection to retrieve the part Response.Write("Hello");
I could get all the information about the method like the class the parameters and everything else but the body i couldn''t

The reflection classes in .net could give you the body as array of bytes[]


Actually it''s an external assembly from the running application
so what i do actually that i load the assembly from assembly object then start to get the information under it

About the possibility to do this ? i think we can cause many reflection application is giving you everything inside the dll the namespaces - classes - methods (exactly as it''s written in the source code )

so any help ?

推荐答案

从哪里来?正在运行或正在某个文件中运行的应用程序?
From where? The application which is running or from some file somewhere?


写道:​​

.net中的反射类可以将您的身体作为字节数组[]

The reflection classes in .net could give you the body as array of bytes[]



我真的不认为这是可能的.此外,当您运行应用程序时,它已转换为CLR特定说明,因此无论如何您都不会获取代码(文本格式).

您可能实际上正在构建ILDASM之类的工具.



I dont really think this is possible. Besides, when you are running your app, its been converted to CLR specific instructions, so you would not be getting your code (in text format) anyway.

You may actually be looking at building a tool like ILDASM.


Reflector可以做到这一点,但是我怀疑他们是通过查看字节数组并从中推断代码来做到这一点的.您必须做类似的事情.

而且您将无法完全按照源代码中的代码"获得它.例如,空格和注释可能不会保留.
Reflector does this, but I suspect they do it by looking at the array of bytes and inferring the code from that. You''d have to do something similar.

And you would not be able to get it "exactly as it''s written in the source code". For example, whitespace and comments would probably not be preserved.


这篇关于获取方法主体作为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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