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 { getset; }
        
public virtual string StudentNo { getset; }
        
public virtual string Name { getset; }
        
public virtual string ClassId { getset; }
        
public virtual string ProfessionId { getset; }
        
public virtual string CollegeId { getset; }
        
public virtual int Phone { getset; }
        
public virtual int Sex { getset; }
        
public virtual string Address { getset; }     }
复制代码

StudentDTO类:

复制代码
public class StudentDTO     {        public StudentDTO()        { 
       }        
public virtual string Id { getset; }
       
public virtual string StudentNo { getset; }
       
public virtual string Name { getset; }
       
public virtual string ClassId { getset; }
       
public virtual string ProfessionId { getset; }
       
public virtual string CollegeId { getset; }
       
public virtual int Phone { getset; }
       
public virtual int Sex { getset; }
       
public virtual int TeacherId { getset; }     }
复制代码

现在创建一个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))
{
//....
}
原文地址:https://www.cnblogs.com/cmblogs/p/3513673.html