Search⌘ K
AI Features

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#.

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 a MemberInfo[] object.

  • GetConstructors(): This returns a ConstructorInfo[] object.

  • GetMethods(): This returns information on MethodInfo[] 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 ...