C#中使用反射顯示程式集的所有類型和屬性

來源:互聯網
上載者:User

  /// <summary>

  ///

  /// loads a *.dll file from txtMethods and invokes all methods int it.

  /// lists all types in the assembly

  ///

  /// </summary>

  /// <param name="sender"></param>

  /// <param name="e"></param>

  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;

  }


作者Blog:http://blog.csdn.net/davidullua/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.