Mega Code Archive

 
Categories / C# / Development Class
 

Demonstrate using Type class to discover information about a class

/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // GetType.cs -- Demonstrate using Type class to discover //               information about a class // //               Compile this program with the following command line: //                   C:>csc GetType.cs using System; using System.Reflection; namespace nsReflection {     class clsEmployee     {         public clsEmployee (string First, string Last, string Zip, int ID)         {             FirstName = First;             LastName = Last;             EmployeeID = ID;             ZipCode = Zip;         }         public string  FirstName;         public string  LastName;         public string  ZipCode;         public int      EmployeeID;         public string Name         {             get {return (FirstName + " " + LastName);}         }         public string Zip         {             get {return (ZipCode);}         }         public int ID         {             get {return (EmployeeID);}         }         static public int CompareByName (object o1, object o2)         {             clsEmployee emp1 = (clsEmployee) o1;             clsEmployee emp2 = (clsEmployee) o2;             return (String.Compare (emp1.LastName, emp2.LastName));         }         static public int CompareByZip (object o1, object o2)         {             clsEmployee emp1 = (clsEmployee) o1;             clsEmployee emp2 = (clsEmployee) o2;             return (String.Compare (emp1.ZipCode, emp2.ZipCode));         }         static public int CompareByID (object o1, object o2)         {             clsEmployee emp1 = (clsEmployee) o1;             clsEmployee emp2 = (clsEmployee) o2;             return (emp1.EmployeeID - emp2.EmployeeID);         }     }     public class GetType     {         static public void Main ()         {             Type t = typeof(clsEmployee);             if (t == null)             {                 Console.WriteLine ("t is null");                 return;             }             Console.WriteLine ("Class clsEmployee is a member of the {0} namespace", t.Namespace);             Console.WriteLine ("\r\nMethods in clsEmployee:");             MethodInfo [] methods = t.GetMethods ();             foreach (MethodInfo m in methods)                 Console.WriteLine ("\t" + m.Name);             Console.WriteLine ("\r\nProperties in clsEmployee:");             PropertyInfo [] props = t.GetProperties ();             foreach (PropertyInfo p in props)                 Console.WriteLine ("\t" + p.Name);             Console.WriteLine ("\r\nFields in clsEmployee:");             FieldInfo [] fields = t.GetFields ();             foreach (FieldInfo f in fields)                 Console.WriteLine ("\t" + f.Name);         }     } }