Type class is an essential class to use while working with a Reflection. Using Type class we can access assembly, module or type run-time information. Accessing these information is simple:
Employee emp1 = new Employee(100, "Mark", 28, 2, 2000);
Type type = Type.GetType("ReflectionTutorial.Employee");
Type type1 = typeof(Employee);
Type type2 = emp1.GetType();
First approach is to use Type’s GetType method. This approach is used if we do not have assembly loaded in our solution. Notice that Type.GetType expects string value, also you need to provide fully qualified name (namespace.typeName).
Second approach would be using typeof operator. Usage is very simple, type keyword typeof followed by Type name in parentheses.
Third approach is convenient if you already have instance of a type. Simply use GetType method. In C# everything is inheriting Object class there fore everything is a class and thus inherits GetType method.
I have acquired type object. What do i do with it now?
Now you can start to use Reflection 🙂 Next post will explain how you can access types Properties.