如何把某一对象x的全部类成员名称都打印出来。能递归打印更好。

解决方案 »

  1.   

    Class1 c = new Class1();
                Type t = c.GetType();
                foreach (MemberInfo info in t.GetMembers())
                {
                    Console.WriteLine(info.Name);
                }
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

    Assembly assembly=Assembly.LoadFrom(fileName); 
     Type[] types=assembly.GetTypes(); 
     for(int i=0;i<types.GetLength(0);++i) 
    { }
    Assembly assembly = Assembly.LoadFrom(toolStripTextBox1.Text);
    foreach (System.Type t in assembly.GetTypes())
     {
      Console.WriteLine(t.Name);
    }
    参考
      

  3.   


    using system; 
    using system.reflection; 
    using system.reflection.emit ; public class testreflection { 
    private string file = @"testreflection.exe"; static void main(string[] args) { 
    testreflection test = new testreflection(); 
    test.displaymodules(); 
    test.displaytypes(); 
    test.displaymethods(); 
    test.invokestaticmethod(); 
    test.invokemethod(); 

    //显示任何模块名 
    public void displaymodules() { 
    console.writeline("display modules ..."); 
    assembly assembly = assembly.loadfrom(file); 
    module[] modules = assembly.getmodules(); 
    foreach( module module in modules ) { 
    console.writeline("module name:" + module.name ); 


    //显示任何类型名 
    public void displaytypes() { 
    console.writeline("display types ..."); 
    assembly assembly = assembly.loadfrom(file); 
    type[] types = assembly.gettypes(); 
    foreach( type type in types ) { 
    console.writeline("type name:" + type.fullname ); 


    //先是个类型中的任何方法名 
    public void displaymethods() { 
    console.writeline("display methods in testreflection type ..."); 
    assembly assembly = assembly.loadfrom(file); 
    type type = assembly.gettype("testreflection"); 
    methodinfo[] methods = type.getmethods(); 
    foreach(methodinfo method in methods) { 
    console.writeline("method name:" + method.name); 


    //调用一个类中的静态方法 
    public void invokestaticmethod() { 
    console.writeline("invoke static method ..."); 
    assembly assembly = assembly.loadfrom(file); 
    type type = assembly.gettype("testsubclass"); 
    type.invokemember ("sayhello", bindingflags.invokemethod,null,null,new object[]{}); 

    //调用一个类中的非静态方法 
    public void invokemethod() { 
    console.writeline("invoke method ..."); 
    assembly assembly = assembly.loadfrom(file); 
    type type = assembly.gettype("testsubclass"); 
    object obj = activator.createinstance(type); 
    testclass tc = (testclass)obj ; 
    type.invokemember ("test1", bindingflags.invokemethod,null,tc,new object[]{}); 
    type.invokemember ("test2", bindingflags.invokemethod,null,tc,new object[]{}); 


    public interface testclass { 
    void test1(); 
    void test2(); 

    public class testsubclass : testclass{ 
    public void test1() { 
    console.writeline("this is testsubclass.test1"); 

    public void test2() { 
    console.writeline("this is testsubclass.test2"); 

    public static void sayhello() { 
    console.writeline("this is testsubclass.sayhello");