C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型。typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称;GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用
typeof是运算符,获得某一类型的System.Type对象;GetType是方法,获取当前实例的类型.
一、typeof 与GetType()的区别
1、Typeof是运算符而是方法
2、GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后)
3、Typeof的参数只能是int,string,String,自定义类型,且不能是实例
4、GetType()和typeof都返回System.Type的引用.
5、TypeOf():得到一个Class的Type
6、 GetType():得到一个Class的实例的Type
二、typeof 与GetType()的实例
实例1:
C# 代码
复制
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
int i = 5;
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Console.WriteLine(i.GetType());//System.Int32
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
var x = 127.25m;
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Console.WriteLine(x.GetType());//System.Decimal
实例2:
C# 代码
复制
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
namespace _2011._12._15
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/ExpandedBlockStart.gif)
{
class Program
{
static void Main(string[] args)
{
Test testone = new Test();
string s = testone.GetType().ToString();
Console.WriteLine("GetType():");
Console.WriteLine(s);//_2011._12._15.Test 命名空间的Test类
Type type = typeof(Test);
Console.WriteLine("Typeof():");
Console.WriteLine(type);//_2011._12._15.Test 命名空间的Test类
Console.WriteLine();
MethodInfo[] methodinfo = type.GetMethods();
Console.WriteLine(methodinfo.GetType());//System.Reflection.MethodInfo[]
foreach (var i in methodinfo)
{
Console.WriteLine(i);//输出Test类的所有方法及继承Object的实例方法
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
MemberInfo[] memberinfo = type.GetMembers();
Console.WriteLine(memberinfo.GetType());
foreach(var i in memberinfo)
{
Console.WriteLine(i);//输出Test类字段和System.type类型
}
}
}
class Test
{ private int age; public string name; public void speaking() { Console.WriteLine("Welcome to cnblog!"); } public void writing() { Console.WriteLine("Please writing something!"); } } }
运行结果:
C# 代码
复制
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
GetType():
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
_2011._12._15.Test
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Typeof():
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
_2011._12._15.Test
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.Reflection.MethodInfo[]
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Void speaking()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Void writing()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.Type GetType()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.String ToString()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Boolean Equals(System.Object)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Int32 GetHashCode()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.Reflection.MemberInfo[]
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Void speaking()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Void writing()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.Type GetType()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.String ToString()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Boolean Equals(System.Object)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Int32 GetHashCode()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Void .ctor()
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.String name