Demonstrate using Type class to discover information about a class : Reflection Assembly « Development Class « C# / C Sharp






Demonstrate using Type class to discover information about a 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);
        }
    }
}


           
       








Related examples in the same category

1.Get Method ParamemtersGet Method Paramemters
2.Get type infomation: base type, is abstract, is com object, is sealed, is classGet type infomation: base type, is abstract, is com object, is sealed, is class
3.Get all methods from a classGet all methods from a class
4.Get all fields from a classGet all fields from a class
5.Get all properties from a classGet all properties from a class
6.Get all implemented interface from a classGet all implemented interface from a class
7.Using reflection to get information about an assembly
8.Demonstrate dynamically invoking an object
9.Locate an assembly, determine types, and create an object using reflection
10.Utilize MyClass without assuming any prior knowledge
11.Uses reflection to show the inherited members of a classUses reflection to show the inherited members of a class
12.Uses reflection to execute a class method indirectlyUses reflection to execute a class method indirectly
13.Deeper Reflection: Invoking Functions
14.Illustrates runtime type invocation
15.Illustrates unloading an application domain
16.Demonstrate typeofDemonstrate typeof
17.Create an object using reflectionCreate an object using reflection
18.Uses an object in another application domain
19.Illustrates creation of an application domainIllustrates creation of an application domain