C#中通过Type类访问数据类型信息

C#中通过Type类可以访问任意数据类型信息。

1.获取给定类型的Type引用有3种方式:
  a.使用typeof运算符,如Type t = typeof(int);
  b.使用GetType()方法,如int i;Type t = i.GetType();
  c.使用Type类的静态方法GetType(),如Type t =Type.GetType("System.Double");
2.Type的属性:
  Name:数据类型名;
  FullName:数据类型的完全限定名,包括命名空间;
  Namespace:数据类型的命名空间;
  BaseType:直接基本类型;
  UnderlyingSystemType:映射类型;
3.Type的方法:
  GetMethod():返回一个方法的信息;
  GetMethods():返回所有方法的信息。

TestType.cs:

01. using  System;
02. using  System.Reflection;
03.  
04. namespace  Magci.Test.Reflection
05. {
06.      public  class  TestType
07.      {
08.          public  static  void  Main()
09.          {
10.              //基本数据类型
11.              Type intType =  typeof ( int );
12.               
13.              //属性
14.              Console.WriteLine( "intType.Name = "  + intType.Name);
15.              Console.WriteLine( "intType.FullName = "  + intType.FullName);
16.              Console.WriteLine( "intType.Namespace = "  + intType.Namespace);
17.              Console.WriteLine( "intType.IsAbstract = "  + intType.IsAbstract);
18.              Console.WriteLine( "intType.IsClass = "  + intType.IsClass);
19.              Console.WriteLine( "intType.IsEnum = "  + intType.IsEnum);
20.              Console.WriteLine( "intType.IsPrimitive = "  + intType.IsPrimitive);
21.              Console.WriteLine( "intType.IsValueType = "  + intType.IsValueType);
22.  
23.              //方法
24.              MethodInfo[] methods = intType.GetMethods();
25.              foreach  (MethodInfo method  in  methods)
26.              {
27.                  Console.WriteLine(method.DeclaringType +  " "  + method.MemberType +  " "  + method.Name);
28.              }
29.          }
30.      }
31. }



TestTypeView.cs:

 

01. using  System;
02. using  System.Text;
03. using  System.Windows.Forms;
04. using  System.Reflection;
05.  
06. namespace  Magci.Test.Reflection
07. {
08.      public  class  TestTypeView
09.      {
10.          public  static  StringBuilder OutputText =  new  StringBuilder();
11.  
12.          public  static  void  Main()
13.          {
14.              Type t =  typeof ( double );
15.              AnalyzeType(t);
16.              MessageBox.Show(OutputText.ToString());
17.          }
18.  
19.          public  static  void  AnalyzeType(Type t)
20.          {
21.              //数据类型名
22.              OutputText.Append( "/nType Name: "  + t.Name);
23.              //数据类型的完全限定名,包括命名空间
24.              OutputText.Append( "/nFull Name: "  + t.FullName);
25.              //数据类型的命名空间
26.              OutputText.Append( "/nNamespace: "  + t.Namespace);
27.               
28.              //直接基本类型
29.              Type tBase = t.BaseType;
30.              if  (tBase !=  null )
31.              {
32.                  OutputText.Append( "/nBase Type: "  + tBase.Name);
33.              }
34.               
35.              //映射类型
36.              Type tUnderlyingSystem = t.UnderlyingSystemType;
37.              if  (tUnderlyingSystem !=  null )
38.              {
39.                  OutputText.Append( "/nUnderlyingSystem Type: "  + tUnderlyingSystem.Name);
40.              }
41.  
42.              //所有公共方法
43.              OutputText.Append( "/n/nPublic members:" );
44.              MemberInfo[] methods = t.GetMethods();
45.              foreach  (MemberInfo method  in  methods)
46.              {
47.                  OutputText.Append( "/n"  + method.DeclaringType +  " "  + method.MemberType +  ""  + method.Name);
48.              }
49.          }
50.      }
51. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值