大体意思是:
给你一个cs(C#)文件,这是一个类文件,编程实现:         0.找出该CS文件中的所有类
         1.找到该类中的所有方法、包括方法的属性,参数等
         2.找到该类的全局变量,包括类型
         3.找到该类的父类(如果有的话)
         4.统计注释行数(暂可以不考虑)
         5.局部变量(暂可以不考虑)
例如:test.cs
      using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;namespace CJStack
{
    public partial class Form1 : Form
    {
        public ArrayList stack = new ArrayList();
        public Graphics g ;
        public Stack stackinout;
        int x = 0;
        /********************************************
         * 用stack变量的入栈出栈:
         * stackinout.push(string)这是入栈
         * stackinout.pop(string)这是出栈,返回栈顶元素,并移除
         * 此题中没用stack型变量,而是用了Arraylist型数组,目的是方便画图和刷新
        ********************************************/
        private void button1_Click(object sender, EventArgs e)
        {
            int len = stack.Count;
            stack.Add(textBox1.Text);
            string s1 = textBox1.Text;
            block(s1);
            textBox1.Text = "";
            textBox3.Text = stack.Count.ToString();
        }
        //绘制进栈的元素,把元素放到block中
        public void block(string str)
        {
            g = CreateGraphics();
            g.FillRectangle(new SolidBrush(Color.Purple), 300, 310-x, 100, 35);
            g.DrawString(str, new Font("宋体", 14), new SolidBrush(Color.Black), new Point(305, 310-x+2));
            x += 37;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int lenth = stack.Count;
            if (lenth ==0)
                MessageBox.Show("此栈已空");
            else
            {
                textBox2.Text = stack[lenth - 1].ToString();
                stack.RemoveAt(lenth - 1);
                textBox3.Text = stack.Count.ToString();
                x = 0;
                if (stack.Count >= 0)
                    g.Clear(BackColor);
                for (int i = 0; i < stack.Count; i++)
                    block(stack[i].ToString());
            }
            Form1_Load(sender, e);
        }
}通过程序得出:
public partial class Form1 
父类:Form
类中方法:
private void button1_Click(object sender, EventArgs e)
public void block(string str)
private void button2_Click(object sender, EventArgs e)
全局变量
public ArrayList stack = new ArrayList();
public Graphics g ;
public Stack stackinout;
int x = 0;
局部变量
麻烦大家说说自己的意见,大家共同探讨下。另外程序比较急,先谢谢大家了。

解决方案 »

  1.   

    这个用反射基本能实现,如果要更强大的功能就要用c#的语法分析器(可用antlr等工具生成)
      

  2.   

    已经实现统计注释行,以及把所有的干扰项目剔除了,哈哈!下一步找出所有的类和方法using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace Class_Reverse
    {
        class CJ 
        {
            public string str = "";
            public Array alist;
            /*声明了连个全局变量*/
            /*
             *以下是这个类的所有方法 
             * 
             */
            public void funone() //第一个类不带参数的
            {
                int i = 0;
                float j = 100;
                //声明了两个局部变量
        string filePath="F://C#//Jackchain";
            }        /// <summary>
            /// 声明了一个带string返回值的类型
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public string FunSecond(string id)
            {
                return id;
            }        void FunThrid()
            {
                FunSecond("1");//调用第二个函数
            }        //这个类结束
        }    //一个新的类开始了
        public class Jackchain : CJ //假设他继承父类Jackchain
        {
            public int a = 0;
            int Jackchain_one()
            {
                return a;
            }
            private void Jackchain_Two(string x)
            {
                string str = a;
            }
            public static string Jackchain_Third()
            {
                return "I am Able!";
            }
            //这个类就到这里吧,结束了
        }
    }
    解析后:class CJ
    {
    public string str = "";
    public Array alist;
    public void funone() 
    {
    int i = 0;
    float j = 100;
    }
    public string FunSecond(string id)
    {
    return id;
    }
    void FunThrid()
    {
    FunSecond("1");
    }
    }
    public class Jackchain : CJ 
    {
    public int a = 0;
    int Jackchain_one()
    {
    return a;
    }
    private void Jackchain_Two(string x)
    {
    string str = a;
    }
    public static string Jackchain_Third()
    {
    return "I am Able!";
    }
    }
    自己顶!!!
      

  3.   


    恩,不过我用过codedom生成代码,没用过codedom分析代码,能行不能行不知道哈哈,听说很强大
      

  4.   

    CodeDom中有ICodeParser接口对代码进行分析,可惜 MS公开的.net类库中没有对ICodeParser的实现
    但是VS可以帮你做到你要的一切 
    看看
    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/5e2c5d94-b90f-45a4-bff1-e888ecd1afb5/
    http://www.codecomments.com/archive358-2006-2-807130.html
      

  5.   

    说白了就是个词法分析器,用C#里反射不好用,只找到public的,而且如果要解释的类是Java的怎么办,就没辙了。我的意思是老老实实得用跟关键字比较,得出,但是这样会很烦,不是最佳方法,好处是不会遗漏。
      

  6.   

    反射怎么做呀?我不懂啊,能讲清楚点吗?
    或者给个例子反射能解析java的类吗?
      

  7.   

    截止到现在,程序已经解决到如下地步:还是9楼那个源程序,现在解析出来的结果如下:代码总行:62  空行:7  注释行:17 
    第1个类
    类:CJ  类型为:方法:funone  返回类型:void 方法类型:public   参数:()
    方法:FunSecond  返回类型:string 方法类型:public   参数:(string id)方法:FunThrid  返回类型:void 方法类型:  参数:()
    第2个类
    类:Jackchain   的父类: CJ  类型为:public
    方法:Jackchain_one  返回类型:int 方法类型:  参数:()方法:Jackchain_Two  返回类型:void 方法类型:private   参数:(string x)方法:Jackchain_Third  返回类型:string 方法类型:public static   参数:()
      

  8.   

    http://www.cnblogs.com/fineboy/archive/2005/09/02/228684.htmlNamespace ReflectionExample 
     { 
      class Class1 
       { 
           [STAThread] 
           static void Main (string [ ] args) 
           { 
               System.Console.WriteLine(“列出程序集中的所有类型”); 
               Assembly a = Assembly.LoadFrom (“ReflectionExample.exe”); 
              Type[ ] mytypes = a.GetTypes( ); 
     
              Foreach (Type t in mytypes) 
             { 
                  System.Console.WriteLine ( t.Name ); 
              } 
              System.Console.ReadLine ( ); 
          System.Console.WriteLine (“列出HellWord中的所有方法” ); 
          Type ht = typeof(HelloWorld); 
          MethodInfo[] mif = ht.GetMethods(); 
         foreach(MethodInfo mf in mif) 
          { 
              System.Console.WriteLine(mf.Name); 
          } 
          System.Console.ReadLine(); 
          System.Console.WriteLine("实例化HelloWorld,并调用SayHello方法"); 
          Object obj = Activator.CreateInstance(ht); 
          string[] s = {"zhenlei"}; 
         Object bojName = Activator.CreateInstance(ht,s); 
          BindingFlags flags = (BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance|BindingFlags.DeclaredOnly); 
          MethodInfo msayhello = ht.GetMethod("SayHello"); 
          msayhello.Invoke(obj,null); 
          msayhello.Invoke(objName,null); 
         System.Console.ReadLine(); 
        } 
        } 
      

  9.   

       我的类是不是当前程序的类,是随便从别的程序拿来的一个类,这样是不是也可以用反射呀?他是不是要先编译下才能用反射呢?换成Java包类呢?
       而且我用反射好像只能获得类中public的方法、变量   不是很懂,望指导。