Case 1:
Copy codeThe Code is as follows: int I = 5;
Console. WriteLine (I. GetType (); // System. Int32
Var x = 127.25 m;
Console. WriteLine (x. GetType (); // System. Decimal
Case 2:Copy codeThe Code is as follows: namespace _ 2011. _ 12. _ 15
{
Class Program
{
Static void Main (string [] args)
{
Test testone = new Test ();
String s = testone. GetType (). ToString ();
Console. WriteLine (s); // _ 2011. _ 12. _ 15. Test class of the namespace
}
}
Class Test
{
}
}
Typeof () returns the object of the class name. You can also return the class name or the methods and fields inside the specified class.Copy codeThe Code is as follows: namespace _ 2011. _ 12. _ 15
{
Class Program
{
Static void Main (string [] args)
{
Test testone = new Test ();
String s = testone. GetType (). ToString ();
Console. WriteLine ("GetType ():");
Console. WriteLine (s); // _ 2011. _ 12. _ 15. Test class of the namespace
Type type = typeof (Test );
Console. WriteLine ("Typeof ():");
Console. WriteLine (type); // _ 2011. _ 12. _ 15. Test class of the namespace
Console. WriteLine ();
MethodInfo [] methodinfo = type. GetMethods ();
Console. WriteLine (methodinfo. GetType (); // System. Reflection. MethodInfo []
Foreach (var I in methodinfo)
{
Console. WriteLine (I); // output all the methods of the Test class and the instance methods that inherit the Object
}
Console. WriteLine ();
Console. WriteLine ();
Console. WriteLine ();
Console. WriteLine ();
MemberInfo [] memberinfo = type. GetMembers ();
Console. WriteLine (memberinfo. GetType ());
Foreach (var I in memberinfo)
{
Console. WriteLine (I); // output the Test class field and System. type
}
}
}
Class Test
{
Private int age;
Public string name;
Public void speaking ()
{
Console. WriteLine ("Welcome to cnblog! ");
}
Public void writing ()
{
Console. WriteLine ("Please writing something! ");
}
}
}
Running result:Copy codeThe Code is as follows: GetType ():
_ 2011. _ 12. _ 15. Test
Typeof ():
_ 2011. _ 12. _ 15. Test
System. Reflection. MethodInfo []
Void speaking ()
Void writing ()
System. Type GetType ()
System. String ToString ()
Boolean Equals (System. Object)
Int32 GetHashCode ()
System. Reflection. MemberInfo []
Void speaking ()
Void writing ()
System. Type GetType ()
System. String ToString ()
Boolean Equals (System. Object)
Int32 GetHashCode ()
Void. ctor ()
System. String name