您的位置:首页 > 编程语言 > C#

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

2009-08-19 11:00 344 查看

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.


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: