.NET Reflection

Reflection uses the type system. Every compiled C# program is encoded into a relational database—this is called metadata. We use reflection to act upon and read the data in this special database. Astonishing features—such as accessing members based on their string names—are brought into existence by the System.Reflection namesapce.
Structurally, metadata is a normalized relational database. This means that metadata is organized as a set of cross-referencing rectangular tables. Lidin, p. 73
As an introduction to reflection, this program uses the System.Reflection namespace to access the metadata of itself. It searches for the public field of the name "_number" and then acquires its value. You can see how a string literal is used to find a variable with that same name.
Program that uses reflection [C#]
using System;
using System.Reflection;
class Program
{
public static int _number = 7;
static void Main()
{
Type type = typeof(Program);
FieldInfo field = type.GetField("_number");
object temp = field.GetValue(null);
Console.WriteLine(temp);
}
}
Output
7
Fields. You can scan or search the fields in your C# program by using the GetField or GetFields methods. The first article provides an example of getting all of the fields in a class and displaying their values. The second article demonstrates how you can actually set the values of fields through reflection.
Reflection Field Example SetValue Reflection Method: FieldInfoMethods. What should you do if you have the name of a method in string format and want to call the method? You can use the GetMethod method and then Invoke the MethodInfo you acquire from it.
MethodInfo Invoke Example GetMethod: Call Method by Name
Properties. You can also scan properties and mutate them using the reflection mechanism in the C# language and .NET Framework, as we demonstrate through the example in the following article.
Reflection Property ProgramThe Type type is one of the most important types when using reflection in the C# language and .NET Framework. In these articles, we discuss aspects of Type, and also the GetType method that is found on the object type. The GetType method helps us discover the object hierarchy.
Type Class GetType Method
Typeof operator. The typeof operator in the C# language actually uses reflection to return the Type of your variable. Also, you will want to use the typeof operator in most of your code that uses more complicated reflection capabilities.
Typeof OperatorThe sizeof operator exposes information about the implementation of types in the .NET Framework to your code. Though it does not require the System.Reflection namespace, it is related to reflection in that it expresses information about the program itself.
Sizeof
Another ability in the C# language that is related to reflection is the default operator. This returns the default value for a type. It does not require the System.Reflection namespace, but can be used when developing classes such as generics.
Default
Programs that use reflection can look inside themselves to see their own inner workings. Though this capacity can lend them unique and powerful features, it also burdens them. Reflection yields programs that are slower and less clear to read, harder to understand.