免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 903 | 回复: 0
打印 上一主题 下一主题

C# 反射类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;


  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string className = "";
  12.             Console.WriteLine("请输入类名(比如System.Int32):");
  13.             className = Console.ReadLine();
  14.             try
  15.             {
  16.                 Type type = Type.GetType(className);
  17.                 Console.WriteLine("类型名:" + type.Name);
  18.                 Console.WriteLine("类的全名:" + type.FullName);
  19.                 Console.WriteLine("命名空间名:" + type.Namespace);
  20.                 Console.WriteLine("程序集名:" + type.Assembly);
  21.                 Console.WriteLine("模块名:" + type.Module);
  22.                 Console.WriteLine("基类名:" + type.BaseType);
  23.                 Console.WriteLine("运行时映射的类名:" + type.UnderlyingSystemType);
  24.                 Console.WriteLine("是否类:" + type.IsClass);
  25.                 Console.WriteLine("是否借口:" + type.IsInterface);
  26.                 Console.WriteLine("是否抽象类:" + type.IsAbstract);
  27.                 Console.WriteLine("是否数组类:" + type.IsArray);
  28.                 Console.WriteLine("是否值类型:" + type.IsValueType);
  29.                 Console.WriteLine("是否基本类型:" + type.IsPrimitive);
  30.                 Console.WriteLine("类的公共成员:");
  31.                 MemberInfo[] mis = type.GetMembers();
  32.                 foreach (MemberInfo mi in mis)
  33.                 {
  34.                     Console.WriteLine("\t{0}:{1},成员名:{2}", mi.MemberType, mi,mi.Name);
  35.                 }
  36.                 
  37.             }
  38.             catch
  39.             {
  40.                 Console.WriteLine("给定的不是一个合法的类名");
  41.             }
  42.         }
  43.     }
  44. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP