• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中使用反射显示程序集的所有类型和属性

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  /// <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;
  }



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap