GetType和typeof的区别
typeof: The typeof operator is used to obtain the System.Type object for a?type.
?
運算符,獲得某一類型的?System.Type?對象。
?
Type t = typeof(int);
?
?
?
GetType: Gets the Type of the current instance.
?
????????????方法,獲取當前實例的類型。
?
?????? ??????int i = 10;
Console.WriteLine(i.GetType());
?
?
區別:
?
- Typeof()是運算符而GetType是方法
- GetType()是基類System.Object的方法,因此只有建立一個實例之后才能夠被調用(初始化以后)
- Typeof()的參數只能是int,string,String,自定義類型,且不能是實例
?
- GetType()?和typeof()都返回System.Type的引用。
?
TypeOf() 和GetType()的區別:??
(1)TypeOf():得到一個Class的Type
(2)GetType():得到一個Class的實例的Type
override 一個方法的前擔是這個方法在父類中:abstract or? virtual, override
?
?
?
GetType()方法的小使用
GetType():獲取當前實例的System.Type.
?現在有兩個類:Student 和 StudentDTO如下:
Student類::
public?class?Student
????{
????????public?Student()
????????{?
????????
????????}
????????public?virtual?string?Id?{?get;?set;?}
????????public?virtual?string?StudentNo?{?get;?set;?}
????????public?virtual?string?Name?{?get;?set;?}
????????public?virtual?string?ClassId?{?get;?set;?}
????????public?virtual?string?ProfessionId?{?get;?set;?}
????????public?virtual?string?CollegeId?{?get;?set;?}
????????public?virtual?int?Phone?{?get;?set;?}
????????public?virtual?int?Sex?{?get;?set;?}
????????public?virtual?string?Address?{?get;?set;?}
????}
StudentDTO類:
public?class?StudentDTO
????{
???????public?StudentDTO()
???????{?
???????}
???????public?virtual?string?Id?{?get;?set;?}
???????public?virtual?string?StudentNo?{?get;?set;?}
???????public?virtual?string?Name?{?get;?set;?}
???????public?virtual?string?ClassId?{?get;?set;?}
???????public?virtual?string?ProfessionId?{?get;?set;?}
???????public?virtual?string?CollegeId?{?get;?set;?}
???????public?virtual?int?Phone?{?get;?set;?}
???????public?virtual?int?Sex?{?get;?set;?}
???????public?virtual?int?TeacherId?{?get;?set;?}
????}
現在創建一個Student:
????????????Student?student?=?new?Student();
????????????student.Id?=?Guid.NewGuid().ToString();
????????????student.Name?=?"張三";
????????????student.StudentNo?=?"T001";
????????????student.Phone?=?10086;
????????????student.Sex?=?1;
????????????student.CollegeId?=?Guid.NewGuid().ToString();
????????????student.ClassId?=?Guid.NewGuid().ToString();
????????????student.ProfessionId?=?Guid.NewGuid().ToString();
????????????student.Address?=?"福建";
現在創建一個Student的DTO類StudentDTO并把Student的信息賦給StudentDTO,常用的方法是:
????????????StudentDTO?studentDTO?=?new?StudentDTO();
????????????studentDTO.Id?=?student.Id;
????????????studentDTO.Name?=?student.Name;
????????????studentDTO.StudentNo?=?student.StudentNo;
????????????studentDTO.Phone?=?student.Phone;
????????????studentDTO.Sex?=?student.Sex;
????????????studentDTO.CollegeId?=?student.CollegeId;
????????????studentDTO.ClassId?=?student.ClassId;
????????????studentDTO.ProfessionId?=?student.ProfessionId;?
而使用GetType()也可以實現:
????????????foreach?(var?item?in?student.GetType().GetProperties())????//返回Student的所有公共屬性
????????????{
????????????????var?value?=?item.GetValue(student,?null);???//返回屬性值????
????????????????var?setobj?=?studentDTO.GetType().GetProperty(item.Name);???//搜索具有指定屬性名稱的公共屬性
????????????????if?(value?!=?null?&&?setobj?!=?null)
????????????????{
????????????????????setobj.SetValue(studentDTO,?value,?null);
????????????????}
????????????}
?
這樣看起來代碼會簡潔些......呵呵,只是個人感覺這個GetType()方法挺好用的就記錄起來了.......
?
在利用多態性時,GetType()是一個有用的方法,允許根據對象的類型來執行不同的代碼,而不是像通常那樣,對所有的對象都執行相同的代碼,
例如,如果一個函數接受一個object類型的參數,就可以在遇到某些對象時執行額外的任務,聯合使用getype() 和 typeof() ,就可以進行比較,如下所示:
?
if(myobj.GetType() == typeof(MyComplexClass))
{
//....
}
總結
以上是生活随笔為你收集整理的GetType和typeof的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Workflow Fou
- 下一篇: 4月人民币国际支付占有率再降!排名也降了