The difference between Object.GetType () and typeof
// operator to obtain a System.Type object of a certain type. typeof(int); // method to get the type of the current instance. intten; Console.WriteLine (I.gettype ());
Difference
Typeof () is an operator and GetType is a method
GetType () is a method of base class System.Object, so only one instance can be called (that is, creating an instance)
The Typeof () parameter can only be lint,string, class, and cannot be an instance
Get the difference between the results
(1) Typeof (): Gets the type of a class
(2) GetType (): Gets the type of a class instance
Use of System.Type.GetType ()
Type type = System.Type.GetType ("consoleapplication1.child"= System.Type.GetType ("system.int32");
Small case of Object.GetType ()
Public classStudent { PublicStudent () {} Public Virtual stringId {Get;Set; } Public Virtual stringStudentno {Get;Set; } Public Virtual stringAddress {Get;Set; } } Public classStudentdto { Publicstudentdto () {} Public Virtual stringId {Get;Set; } Public Virtual stringStudentno {Get;Set; } Public Virtual intTeacherid {Get;Set; } }//Assigning a value to a student objectStudent Student =NewStudent (); Student. Id=Guid.NewGuid (). ToString (); Student. Name="Zhang San"; Student. Address="Fujian";//Assigns the value of student to StudentdtoStudentdto studentdto =Newstudentdto (); Studentdto.id=student. Id; Studentdto.name=student. Name: If there are too many properties for student, you can reduce the number of code by this methodforeach(varIteminchStudent. GetType (). GetProperties ())//returns all public properties of student { varValue = Item. GetValue (student,NULL);//Return property value varSetobj = Studentdto.gettype (). GetProperty (item. Name);//Search for public properties with the specified property name if(Value! =NULL&& Setobj! =NULL) {setobj. SetValue (studentdto, value,NULL); } }
Comparison of C # 's System.Type.GetType () and Object.GetType () with TypeOf