获取一个类的所有属性、方法的名字

来源:互联网 发布:量化数据举例 编辑:程序博客网 时间:2024/05/02 16:16

use reflection:
---------------------
namespace reflect
{
using System ;
using System.Reflection ;

public class ClassA
{

  public static int Main ( string[] kami )
  {
    Type t = typeof( test ) ;
    Console.WriteLine ( "Type of class: " + t ) ;
    Console.WriteLine ( "Namespace: " + t.Namespace ) ;
    ConstructorInfo[] ci = t.GetConstructors( );
    Console.WriteLine( "Constructors are:" ) ;
 
    foreach( ConstructorInfo i in ci )
    {
     Console.WriteLine( i ) ;
    }
 
    PropertyInfo[] pi = t.GetProperties( );
    Console.WriteLine( "Properties are:" ) ;
 
    foreach( PropertyInfo i in pi )
    {
     Console.WriteLine( i ) ;
    }
    MethodInfo[] mi = t.GetMethods( ) ;
    Console.WriteLine( "Methods are:" ) ;
 
    foreach( MethodInfo i in mi )
    {
     Console.WriteLine( "Name: " + i.Name ) ;
     ParameterInfo[] pif = i.GetParameters () ;
     foreach ( ParameterInfo p in pif )
    {
      Console.WriteLine("Type: " + p.ParameterType + " parameter name: " + p.Name ) ;
    }
}
return 0 ;
}


public class test
{
  int i ;
  int prop;

  public int Prop
  {
    get
  {

  return prop ;
 }

set
{
  prop = value ;
}

}
 
 
public test()
{
}
 
 
  public test ( int x )
  {
    i = x ;
  }

  public int funcA ( int x )
  {
    Console.WriteLine ( x ) ;
    return 2 * x ;
   }
}
}
}