分享

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

 jicemoon 2015-06-19

Type 类

表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。

Type 初始化 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:
  1. using System;  
  2. using System.Reflection;  
  3.   
  4. namespace Magci.Test.Reflection  
  5. {  
  6.      public class TestType  
  7.      {  
  8.          public static void Main()  
  9.          {  
  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:
  1. using System;  
  2. using System.Text;  
  3. using System.Windows.Forms;  
  4. using System.Reflection;  
  5.   
  6. namespace Magci.Test.Reflection  
  7. {  
  8.      public class TestTypeView  
  9.      {  
  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. }  

  1. 轉自:<a href="http://apps.hi.baidu.com/share/detail/34081693">http://apps.hi.baidu.com/share/detail/34081693</a>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多