现在需要利用codedom分析一个cs文件,由于从未接触过codedom,因此感觉无从下手!
高手可否讲解一下,给出一点示例代码!

解决方案 »

  1.   

    http://www.15seconds.com/issue/020917.htmhttp://www.15seconds.com/issue/021113.htm
      

  2.   

    To saucer(思归):   我看了你给的链接,好像那两个都是生成.cs文件吧!  
      我想要做的是对已经存在的.cs文件进行分析,提取出
      所有的函数名等信息!
      

  3.   

    use CompileAssemblyFromFile, the use Reflection, for example
    using System;
    using System.Text;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;
    class TestCode
    {
      void Test() {}
      static void Main(string[] args)
      {
    string filename = args[0]; CSharpCodeProvider provider = new CSharpCodeProvider();
    ICodeCompiler compiler = provider.CreateCompiler();
    CompilerParameters compilerparams = new CompilerParameters(new string[]{"System.dll","cscompmgd.dll"});
    compilerparams.GenerateExecutable = false;
    compilerparams.GenerateInMemory = true;
    CompilerResults results = compiler.CompileAssemblyFromFile(compilerparams, filename); if (results.Errors.HasErrors)
         {
             StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
             foreach (System.CodeDom.Compiler.CompilerError error in results.Errors )
             {
                 errors.AppendFormat("Line {0},{1}\t: {2}\n", 
                        error.Line, error.Column, error.ErrorText);
             }
             Console.WriteLine(errors.ToString());
         }
         else
         {
             Assembly a = results.CompiledAssembly;
    foreach(Type t in a.GetTypes())
    {
    Console.WriteLine("Type:{0}", t.Name);
    foreach(MethodInfo mi in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
    Console.WriteLine("method:{0}", mi.Name);
    Console.WriteLine();
    }
         }
      }
    }