c#中GetType()與Typeof()的區(qū)別
案例1:
int i = 5;
Console.WriteLine(i.GetType());//System.Int32
var x = 127.25m;
Console.WriteLine(x.GetType());//System.Decimal
案例2:
namespace _2011._12._15
{
class Program
{
static void Main(string[] args)
{
Test testone = new Test();
string s = testone.GetType().ToString();
Console.WriteLine(s);//_2011._12._15.Test 命名空間的Test類
}
}
class Test
{
}
}
Typeof()返回的是類名的對象,也可以返回類名,也可以返回特定類內(nèi)部的方法和字段
namespace _2011._12._15
{
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的實(shí)例方法
}
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!");
}
}
}
運(yùn)行結(jié)果:
GetType():
_2011._12._15.Test
Typeof():
_2011._12._15.Test
System.Reflection.MethodInfo[]
Void speaking()
Void writing()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Reflection.MemberInfo[]
Void speaking()
Void writing()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
Void .ctor()
System.String name
相關(guān)文章
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘五 棧和隊(duì)列
這節(jié)我們討論了兩種好玩的數(shù)據(jù)結(jié)構(gòu),棧和隊(duì)列2012-11-11
C#設(shè)置或驗(yàn)證PDF文本域格式的方法詳解
PDF中的文本域可以通過設(shè)置不同格式,用于顯示數(shù)字、貨幣、日期、時(shí)間、郵政編碼、電話號(hào)碼和社保號(hào)等等。本文將介紹如何通過C#設(shè)置或驗(yàn)證PDF文本域格式,需要的可以參考一下2022-01-01
C#調(diào)用C動(dòng)態(tài)鏈接庫的實(shí)現(xiàn)
動(dòng)態(tài)鏈接庫是不能直接執(zhí)行的,也不能接收消息,它只是一個(gè)獨(dú)立的文件,本文主要介紹了C#調(diào)用C動(dòng)態(tài)鏈接庫的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
C# Winform 讓整個(gè)窗口都可以拖動(dòng)
Windows 的 API 果然強(qiáng)大啊.以前要實(shí)現(xiàn)全窗口拖動(dòng), 要寫鼠標(biāo)按下和抬起事件, 很是麻煩, 偶爾還會(huì)出現(xiàn) BUG2011-05-05
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘三 鏈表
這節(jié)我們討論鏈表的基本操作,并且畫圖以證明,下屆中我們將討論雙向鏈表,環(huán)形鏈表 應(yīng)用舉例2012-11-11
AOP從靜態(tài)代理到動(dòng)態(tài)代理(Emit實(shí)現(xiàn))詳解
AOP為Aspect Oriented Programming的縮寫,意思是面向切面編程的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于AOP從靜態(tài)代理到動(dòng)態(tài)代理(Emit實(shí)現(xiàn))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09

