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

来源:网络整理 责任编辑:栏目编辑 发表时间:2013-07-02 01:58 点击:

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

  &nb

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 用C#制作屏幕捕获程序
    • .NET程序员项目开发必知必会—Dev环境中的集成测试用例执行时上
    • 遍历ArrayList易犯错误
    • C#对XML操作:一个处理XML文件的类(1)
    • .NET简谈反射(动态调用)
    • 使用C#编写LED样式时钟控件
    • DataList嵌套问题 如何删除内层子DataList的记录
    • 怎样用C#实现完整文档打印功能
    • .NET简谈自定义事务资源管理器
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1