Introduction to Reflection
Explore how to use .NET Reflection to discover type information during program execution. Understand obtaining type objects, inspecting members, and accessing assembly metadata to analyze and manipulate types dynamically in C#.
We'll cover the following...
Every application is composed of classes and other types, along with their methods, properties, and indexers. Reflection lets us obtain type information while the program is running. For instance, we could discover what methods a class has, what properties it operates, and what interfaces it implements. Reflection is type discovery at runtime.
The reflective functionality of .NET is located in the System.Reflection namespace. To obtain information on a type, we use the System.Type class. This class has methods like:
GetMembers(): This returns aMemberInfo[]object.GetConstructors(): This returns aConstructorInfo[]object.GetMethods(): This returns information onMethodInfo[]methods.
While the Type class exposes many members, these methods provide a general idea of its capabilities.
Obtain type information
To obtain information on a type, we must first get an instance of the Type class representing that type. We can do that in ...