Maybe you are new to the .Net lifestyle or maybe you are a veteran, either way, you ended up here to get a better understanding of what reflection is all about. To begin reflection is the process of describing various metadata of types, methods, and fields in code, that is literal Microsoft definition but no worries I will explain it. So before we dive into anything else you should already have an understanding of namespaces are and why we use them.
With reflection in C#, you can dynamically create an instance of a type and bing that type to an existing object. Moreover, you can get the type from an existing object and access its properties. When you use attributes in your code, reflection gives you access as it provides objects of the type that describe modules, assemblies, and types.
So in this article, I’ll discuss C# Reflection with various examples. Here we’ll learn the way of getting type information using different ways and use of properties and methods of C# Reflection type class.
Here’s a simple example of reflection using the static method GetType – inherited by all types from the Object base class – to obtain the type of a variable,
//using GetType to obtain type information
int a = 42;
System.Type type = a.GetType();
System.Console.WriteLine(type);
//the output will display as the following
//System.Int32
Defining Reflection in C#
To understand reflection, there are a few basics you should understand about modules, types, and members:
- Assemblies contain modules
- Modules contain types
- Types contain members
You need to use Reflection when you want to inspect the contents of an assembly. For example, you can get all members of the object by typing “.” before an object when viewing your Visual Studio editor IntelliSense.
A program reflects on itself when it extracts metadata from its assemblies, then uses it to modify its behavior or inform the user. When you write a C# program that uses reflection, you can use either the TypeOf operator or the GetType() method to get the object’s type.
Examples of Reflection in C#
Implementing reflection in C# requires a two-step process. You first get the “type” object, then use the type to browse members such as “methods” and “properties”. This is how you would create instances of DateTime class from the system assembly:
//create instance of class DateTime
DateTime dt = (DateTime) Activator.CreateInstance(typeof(DateTime);
Namespace Test
{
public class YuGiOh
{
public YuGiOh() {...}
public string name
public static double Attack {get{...} set{...}}
public static double Defense {get{...} set{...}}
public static string Attribute() {...}
public static string MonsterEffect() {...}
public static int Stars () {get{...} set{...}}
public static string CardType() {...}
}
}
//dynamically load assembly file
Assembly yugioh = Assembly.LoadFile(@"c:'Test.dll")
//get type of class YuGiOh from just loaded assembly
Type card = yugioh.GetType("Test.YuGiOh");
//create instance of class YuGiOh
object cardInstance = Activator.CreateInstance(card);
And access its members (the following examples illustrate getting values for the public double Number property):
//get info about property: public double Attack
PropertyInfo cardPropertyInfo = card.GetProperty("Attack");
//get the value of property: public double Attack
double value = (double)cardPropertyInfo.GetValue(cardInstance, null);
//set value of property: public double Attack
cardPropertyInfo.SetValue(cardInstance, 10.0, null);
Then, you
Understanding How Reflection Works
The main class for reflection is the System.Type class, which is an abstract class representing a type in the Common Type System. When you use this class, you can find the types used in a module and namespace and also determine if a given type is a reference or value type. You can parse the corresponding metadata tables to look through these items:
- Fields
- properties
- Methods
- Events
Understanding How Reflection in C# Works
There are several uses including:
- Use Module to get all global and non-global methods defined in the module.
- Use MethodInfo to look at information such as parameters, name, return type, access modifiers, and implementation details.
- Use EventInfo to find out the event-handler data type, the name, declaring type and custom attributes.
- Use ConstructorInfo to get data on the parameters, access modifiers, and implementation details of a constructor.
- Use Assembly to load modules listed in the assembly manifest.
- Use PropertyInfo to get the declaring type, reflected type, data type, name and writable status of a property or to get and set property values.
- Use CustomAttributeData to find out information on custom attributes or to review attributes without having to create more instances.
Other uses for Reflection include constructing symbol tables, to determine which fields to persist and through serialization.