163K网站系统官方论坛's Archiver

zzsh 发表于 2007-2-16 18:20

.net教程:c#中使用反射显示程序集的所有类型和属性

[size=3] private void btnlist_click(object sender, system.eventargs e)
  {
   string filename=labelfile.text.trim();
   string result="";
   txtmethods.text="";

   txttypes.text="";
   if(file.exists(filename))
   {
    try
    {
     assembly assembly=assembly.loadfrom(filename);
     type[] types=assembly.gettypes();
     result="the assembly contains the following types :"+environment.newline;
   
     for(int i=0;i<types.getlength(0);++i)
     {
      result+="\t "+i+":"+types[i].name+" "+" "+environment.newline;
      // get the public methods.
      methodinfo[] myarraymethodinfo=types[i].getmethods(bindingflags.public|bindingflags.instance|bindingflags.declaredonly);
      txtmethods.text=txtmethods.text+environment.newline+"the number of public methods in "+types[i].name+" is "+myarraymethodinfo.length+environment.newline;
      // get all the methods.
      txtmethods.text=txtmethods.text+getmethodinfo(myarraymethodinfo);
      /*
      // get the nonpublic methods.
      methodinfo[] myarraymethodinfo1 = mytype.getmethods(bindingflags.nonpublic|bindingflags.instance|bindingflags.declaredonly);
      console.writeline("\nthe number of protected methods is {0}.", myarraymethodinfo1.length);

      // display information for all methods.
      labelfile.text=displaymethodinfo(myarraymethodinfo1);
      */
     }
     foreach(type mytype in types)
     {
      // get the public properties.
      propertyinfo[] mypropertyinfo = mytype.getproperties(bindingflags.public|bindingflags.instance);
      console.writeline("the mumber of public properties in "+mytype.name+" is {0}.", mypropertyinfo.length);
      // display the public properties.
      getpropertyinfo(mypropertyinfo);
      // get the nonpublic properties.
      propertyinfo[] mypropertyinfo1 = mytype.getproperties(bindingflags.nonpublic|bindingflags.instance);
      txtmethods.text=txtmethods.text+environment.newline+("the number of nonpublic properties in "+mytype.name+" is "+ mypropertyinfo1.length)+environment.newline;
      // display all the nonpublic properties.
      txtmethods.text=txtmethods.text+getpropertyinfo(mypropertyinfo1);
     }
     txttypes.text=result;
    }
    catch(exception ee)
    {
     throw ee;
    }
   
   }
  }
  /// <summary>
  /// get method informations from methodinfo[] array:
  /// </summary>
  /// <param name="myarraymethodinfo"></param>
  /// <returns></returns>
  public string getmethodinfo(methodinfo[] myarraymethodinfo)
  {
   string methodstr="";
   ///
   ///getinformation for all methods.
   for(int i=0;i<myarraymethodinfo.length;i++)
   {
    methodinfo mymethodinfo = (methodinfo)myarraymethodinfo[i];
    methodstr+="method "+i+" :"+ mymethodinfo.name+environment.newline;
   }   
   return methodstr;
  }
  /// <summary>
  /// get properties information from propertyinfo[] array:
  /// </summary>
  /// <param name="mypropertyinfo"></param>
  /// <returns></returns>
  public string getpropertyinfo(propertyinfo[] mypropertyinfo)
  {
   string propstr="";
   // display information for all properties.
   for(int i=0;i<mypropertyinfo.length;i++)
   {
    propertyinfo mypropinfo = (propertyinfo)mypropertyinfo[i];
    propstr+="property "+i+":"+ mypropinfo.name+" type:"+ mypropinfo.propertytype+environment.newline;
   }
   return propstr;
  }



  



[/size]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.