Reflection in C# using the Type Class: Runtime Type Inspection and Manipulation
Explore reflection in C#, a powerful technique for inspecting and manipulating types and their members at runtime. This guide explains the `Type` class, its methods, and how to use reflection for tasks like type checking, invoking methods dynamically, and accessing member information.
Reflection in C# Using the `Type` Class
Reflection in C# is a powerful feature that allows you to inspect and manipulate types and their members (methods, properties, fields, etc.) at runtime. The `Type` class is central to this process, providing methods and properties to access type information dynamically.
Understanding Reflection
Reflection is the ability of a program to examine its own structure and behavior while it's running. It's like having a mirror that shows you the internal workings of your code. This is useful for a variety of tasks, including:
- Dynamically creating and working with objects.
- Inspecting the members of a class.
- Invoking methods.
- Building flexible and extensible applications.
The `Type` Class
The `Type` class (in the `System` namespace) represents a type in the Common Type System (CTS). You can obtain a `Type` object using `typeof(MyClass)` or `myObject.GetType()`. The `Type` object provides methods to access its members and properties.
Key Properties of the `Type` Class
Property | Description |
---|---|
Assembly |
Gets the assembly containing the type. |
AssemblyQualifiedName |
Gets the fully qualified name, including assembly information. |
Attributes |
Gets attributes applied to the type. |
BaseType |
Gets the base type (if any). |
FullName |
Gets the fully qualified name of the type. |
IsAbstract , IsArray , IsClass , IsEnum , IsInterface , IsNested , IsPrimitive , IsPointer , IsNotPublic , IsPublic , IsSealed , IsSerializable |
Boolean properties indicating type characteristics. |
MemberType |
Indicates the member type (if nested). |
Module |
Gets the module containing the type. |
Name |
Gets the simple name of the type. |
Namespace |
Gets the namespace of the type. |
Key Methods of the `Type` Class
Method | Description |
---|---|
GetConstructors() , GetConstructors(BindingFlags) |
Retrieves constructor information. |
GetFields() , GetFields(BindingFlags) |
Retrieves field information. |
GetMembers() , GetMembers(BindingFlags) |
Retrieves all members (methods, fields, properties, etc.). |
GetMethods() , GetMethods(BindingFlags) |
Retrieves method information. |
GetProperties() , GetProperties(BindingFlags) |
Retrieves property information. |
GetType() |
Gets the type of the current instance. |
GetType(string typeName) |
Gets the type with the specified name. |
Examples
Example 1: Getting Type Information
Type myType = typeof(int);
Console.WriteLine(myType); //Output: System.Int32
Example 2: Getting Assembly Information
Type myType = typeof(string);
Console.WriteLine(myType.Assembly); // Output: mscorlib
Example 3: Printing Type Details
Type myType = typeof(string);
Console.WriteLine(myType.FullName);
Console.WriteLine(myType.BaseType);
Console.WriteLine(myType.IsClass);
Example 4: Getting Constructors
Type myType = typeof(string);
ConstructorInfo[] constructors = myType.GetConstructors();
foreach (ConstructorInfo constructor in constructors) {
Console.WriteLine(constructor);
}
Example 5: Getting Methods
Type myType = typeof(string);
MethodInfo[] methods = myType.GetMethods();
foreach (MethodInfo method in methods) {
Console.WriteLine(method);
}
Example 6: Getting Fields
Type myType = typeof(string);
FieldInfo[] fields = myType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
foreach (FieldInfo field in fields) {
Console.WriteLine(field);
}