typeof: The typeof operator is used to obtain the System.Type object for a type.
Operator, to achieve a certain type of System.Type objects.
Type t = typeof(int);
GetType: Gets the Type of the current instance.
The method of obtaining the type of the current instance .
int i = 10;
Console.WriteLine(i.GetType());
the difference:
- Typeof () is an operator and a method GetType
- The GetType () method of the base class System.Object, so only after the establishment of an instance to be able to be called (after initialization)
- Typeof () parameter only int, string, String, custom type, and can not be examples
- GetType () and typeof () returns a reference to both the System.Type.
TypeOf () and GetType () the difference:
(1) TypeOf (): Get a Class of Type
(2) GetType (): to give an example of a Class of Type
Before the catalyst is a method override this method in the parent class: abstract or virtual, override
GetType (): Gets System.Type of the current instance.
There are two classes: Student and StudentDTO follows:
Student class ::
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 categories:
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; }
}
Now create a 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 = "福建";
Now create a Student of DTO class StudentDTO and the information Student assign StudentDTO, commonly used method is:
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;
The use GetType () can be achieved:
foreach (. var item in student.GetType ( ) GetProperties ()) // return all the public properties Student
{
var value = item.GetValue (Student, null); // returns the property value
var setobj = studentDTO.GetType (). GetProperty (item.Name); // common attribute with the specified search attribute name
IF (= null value setObj = null &&!!)
{
setobj.SetValue (studentDTO, value, null);
}
}
This looks more concise code ...... Oh, just a personal feeling that GetType () method with a very good record on up .......
When Polymorphism, the GetType () is a useful method that allows to execute different code depending on the type of the object, rather than as usual, the same code is executed for all the objects,
For example, if a function accepts an object of type parameters, can be performed in the face of some objects additional tasks, in combination getype () and typeof (), can be compared as follows:
if(myobj.GetType() == typeof(MyComplexClass))
{
//....
}