c# Type类型

一、概述

Type表示类型,可以获得:类、接口、数组、值、枚举、类型参数、泛型等类的类型。我们可以通过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");//获取具有指定名称的type,该方法有六个重载。
2.Type的属性:
   Name:数据类型名;
   FullName:数据类型的完全限定名,包括命名空间;
   Namespace:数据类型的命名空间;
   IsClass:是否是引用类型;
   IsValueType:是否是值类型;
   BaseType:直接基本类型,即直接父类。
   UnderlyingSystemType:映射类型;
   注:映射类型指的是CTS(通用类型系统)中的类型。CTS保证了不同程序语言的对象能够交互,例如:VB中integer和C#中的int32最后都会转换为CTS中的system.int32类型。
3.Type的方法:
   GetMethod(string MethodName):返回某个方法的信息;
   GetMethods():返回所有公共方法的信息。
   GetFields():返回所有公共字段信息。
   GetMembers();返回所有的公共成员信息,包括字段,属性,方法,事件等。
    。。。
  4.下面看一个关于Type的例子
  TestType.cs:
[c#]  view plain copy
  1. using System;  
  2. using System.Reflection;  
  3.   
  4. namespace gameloft9
  5. {  
  6.      public class TestType  
  7.      {  
  8.          public static void Main()  
  9.          {  
  10.              //基本数据类型  
  11.               Type t = typeof(int);  
  12.             // t=Type.GetType("int");
  13.              //int i;
  14.              //t = i.GetType();
  15.              //属性  
  16.              Console.WriteLine("intType.Name = " + intType.Name);  
  17.              Console.WriteLine("intType.Namespace = " + intType.Namespace);  
  18.              Console.WriteLine("intType.IsClass = " + intType.IsClass);  
  19.              Console.WriteLine("intType.IsValueType = " + intType.IsValueType);  
  20.   
  21.              //方法  
  22.             /*MethodInfo属于System.Reflection空间,同样还有:
  23.       FieldInofSystem.Reflection.Assembly 
          System.Reflection.MemberInfo
          System.Reflection.EventInfo
          System.Reflection.FieldInfo
          System.Reflection.MethodBase
          System.Reflection.ConstructorInfo
          System.Reflection.PropertyInfo*/
  24.              MethodInfo[] methods = intType.GetMethods();  
  25.              foreach (MethodInfo i in methods)  
  26.              {  
  27.                  //申明该类型的类+成员类型(这里肯定是method类型)+方法名
  28.                  Console.WriteLine(i.DeclaringType + " " + i.MemberType + " " + i.Name);  
  29.              }  
  30.          }  
  31.      }  
  32. }  
  33. 关于反射可以参考博客:
  34. http://blog.csdn.net/tonglian/article/details/2271066
  35. 参考博客:
  36. http://blog.csdn.net/byondocean/article/details/6550457
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值