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():获取当前实例的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; } }
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; } }
} 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()是一个有用的方法,允许根据对象的类型来执行不同的代码,而不是像通常那样,对所有的对象都执行相同的代码,
例如,如果一个函数接受一个object类型的参数,就可以在遇到某些对象时执行额外的任务,联合使用getype() 和 typeof() ,就可以进行比较,如下所示:
if(myobj.GetType() == typeof(MyComplexClass))
{
//....
}