天天看點

C#中的TypeType 類

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():傳回所有方法的資訊。

using System;
using System.Reflection;

namespace Magci.Test.Reflection
{
     public class TestType
     {
         public static void Main()
         {
             //基本資料類型
             Type intType = typeof(int);
            
             //屬性
             Console.WriteLine("intType.Name = " + intType.Name);
             Console.WriteLine("intType.FullName = " + intType.FullName);
             Console.WriteLine("intType.Namespace = " + intType.Namespace);
             Console.WriteLine("intType.IsAbstract = " + intType.IsAbstract);
             Console.WriteLine("intType.IsClass = " + intType.IsClass);
             Console.WriteLine("intType.IsEnum = " + intType.IsEnum);
             Console.WriteLine("intType.IsPrimitive = " + intType.IsPrimitive);
             Console.WriteLine("intType.IsValueType = " + intType.IsValueType);

             //方法
             MethodInfo[] methods = intType.GetMethods();
             foreach (MethodInfo method in methods)
             {
                 Console.WriteLine(method.DeclaringType + " " + method.MemberType + " " + method.Name);
             }
         }
     }
}           
using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Magci.Test.Reflection
{
     public class TestTypeView
     {
         public static StringBuilder OutputText = new StringBuilder();

         public static void Main()
         {
             Type t = typeof(double);
             AnalyzeType(t);
             MessageBox.Show(OutputText.ToString());
         }

         public static void AnalyzeType(Type t)
         {
             //資料類型名
             OutputText.Append("/nType Name: " + t.Name);
             //資料類型的完全限定名,包括命名空間
             OutputText.Append("/nFull Name: " + t.FullName);
             //資料類型的命名空間
             OutputText.Append("/nNamespace: " + t.Namespace);
            
             //直接基本類型
             Type tBase = t.BaseType;
             if (tBase != null)
             {
                 OutputText.Append("/nBase Type: " + tBase.Name);
             }
            
             //映射類型
             Type tUnderlyingSystem = t.UnderlyingSystemType;
             if (tUnderlyingSystem != null)
             {
                 OutputText.Append("/nUnderlyingSystem Type: " + tUnderlyingSystem.Name);
             }

             //所有公共方法
             OutputText.Append("/n/nPublic members:");
             MemberInfo[] methods = t.GetMethods();
             foreach (MemberInfo method in methods)
             {
                 OutputText.Append("/n" + method.DeclaringType + " " + method.MemberType + "" + method.Name);
             }
         }
     }
}