如题 比如说我有一个
public void output()
{
   Console.writeline("hello");}public void method()
{
    //在这里我想把output这个方法以字符串的形式表示显示出来,怎么做呀?
}大家帮帮我

解决方案 »

  1.   

    这是获取方法名        static void Main(string[] args)
            {            MethodInfo[] mInfos = typeof(A).GetMethods();
                foreach (MethodInfo method in mInfos)
                    Console.WriteLine("method:{0}",method.Name);
            }类A    public class A
        {
            
            public void f() { int i = 0; }
        }
      

  2.   

    我要取得方法体 即 “public void output()
    {
      Console.writeline("hello");} ” 这样的字符串 
      

  3.   

    可以利用Reflector.exe把在项目里直接引用Reflector.exe
    然后使用
    using Reflector;
    using Reflector.CodeModel;
    就可以是其提供的反编译功能,实现你的东西具体可以参考
    http://www.cnblogs.com/worksguo/articles/988180.html
    http://www.cnblogs.com/smalldust/archive/2006/08/30/490742.html
      

  4.   

     
    我用Reflector.exe做了一个简单的例子,基本上可以把当前的反编译出来,不过在界面显示上没优化,看起来有点乱  
     public partial class Form1 : Form
        {
            IServiceProvider serviceProvider;
            ILanguageManager languageManager;
                 public Form1()
            {
                InitializeComponent();
            }        private  void Form1_Load(object sender, EventArgs e)
            {
                serviceProvider = new ApplicationManager(null);
                languageManager = (ILanguageManager)serviceProvider.GetService(typeof(ILanguageManager));
               
                IAssemblyManager assemblyManager = (IAssemblyManager)serviceProvider.GetService(typeof(IAssemblyManager));
                var a = this.GetType();
                IAssembly assembly = assemblyManager.LoadFile(this.GetType().Module.FullyQualifiedName);            StringBuilder output = new StringBuilder();
               
                output.AppendLine("装配件: " + assembly.ToString());            foreach (IModule module in assembly.Modules)
                {
                    output.AppendLine(("模块: " + module.Name));                foreach (ITypeDeclaration typeDeclaration in module.Types)
                    {
                        output.AppendLine(("类型: " + typeDeclaration.Namespace + "." + typeDeclaration.Name));                    foreach (IMethodDeclaration methodDeclaration in typeDeclaration.Methods)
                        {
                            output.AppendLine(("方法: " + methodDeclaration));                        IMethodBody methodBody = methodDeclaration.Body as IMethodBody;
                            if (methodBody != null)
                            {
                                foreach (IInstruction instruction in methodBody.Instructions)
                                {
                                    output.Append("L" + instruction.Offset.ToString("X4", CultureInfo.InvariantCulture));
                                    output.Append(": ");
                                    //output.Append(InstructionTable.GetInstructionName(instruction.Code));
                                    output.Append(instruction.Code);
                                    
                                    if (instruction.Value != null)
                                    {
                                        output.Append(" ");                                    if (instruction.Value is string)
                                        {
                                            output.Append("\"");
                                        }                                    output.Append(instruction.Value.ToString());                                    if (instruction.Value is string)
                                        {
                                            output.Append("\"");
                                        }
                                    }                                output.AppendLine();
                                }
                            }
                        }
                    }
                }            this.richTextBox1.Clear();
                this.richTextBox1.Text = output.ToString();
     
                
            }
        }}