打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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>  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ahjesus约束方法或属性的调用方
Reflect中MethodInfo使用方法
C#动态方法调用
通过Type.InvokeMethod实现方法的重载
反射(转)
C# 中的泛型
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服