(C# Language) Reflection

Reflection is used to obtain information about the functions, properties, events, fields of a given Type of dotnet. For example, we can use reflection to query the constructors and methods of the System.String class. We can even use Reflection to execute the methods queried through Reflection. We can use Reflection to obtain a description about the assembly that contains a given Type. Please do, however, take note that this is not something that is done very commonly - it's available when you need it.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

How does Reflection work?

Reflection works through the System.Type class. We can obtain the data-type of an object by using the GetType method. In this program we have a variable y. The function GetType provides us the System.Type object for this instance. Once you have the System.Type object, then you can use it to obtain information about the properties, methods and other members of the data-type behind the variable y. The Name property of the type object provides a readable name of the data-type. The console writeline prints System.Int32.


int y = 10;

Type type = y.GetType();

// prints System.Int32 
Console.WriteLine(type.Name);

Video Explanation (see it happen!)

Please watch the following youtube video:

typeof Operator

It is possible to obtain the System.Type instance from the data-type directly - an instance such as y is not required.


Type type2 = typeof(string);

For this, use the typeof operator - like typeof(string).

Querying the constructors

Let's now query the constructors of the System.String class.


Type type2 = typeof(string);

ConstructorInfo[] ci = type2.GetConstructors();

Console.WriteLine("Constructors ...");

foreach (ConstructorInfo m in ci)
{
  Console.WriteLine(m);
}

Console.WriteLine();

Collection of ConstructorInfo is obtained with the GetConstructors method.

A for-each loop can be used to print all the constructors.


// output 

Void .ctor(Char[])
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
Void .ctor(Char, Int32)
Void .ctor(System.ReadOnlySpan`1[System.Char])

Querying the Members

Let's now query everything in the System.String class - contructors, functions, properties, events.


// everything - properties, methods,  
// ctors, events 
MemberInfo[] members = type2.GetMembers();

foreach (MemberInfo m in members)
{
  Console.WriteLine(m);
}

Console.WriteLine();

For this we obtain the collection of MemberInfo associated with the Type by using the GetMembers function.

Next use a for-each loop to print each MemberInfo. A long list is printed on the console.


// output similar to 

Int32 LastIndexOf(Char)
Int32 LastIndexOf(Char, Int32)
Int32 LastIndexOf(Char, Int32, Int32)
Int32 LastIndexOfAny(Char[])
Int32 LastIndexOfAny(Char[], Int32)
Int32 LastIndexOfAny(Char[], Int32, Int32)

Querying a Method and Calling it

Use the GetMethod function for obtaining the MethodInfo about the function. The GetMethod function requires the name of the function you wish to query.


MethodInfo? method = type2.GetMethod("IsNullOrEmpty");

object? result = method?.Invoke(type2, new object[] { "abc" });

Console.WriteLine(result);

After this use the Invoke function by passing an array of object arguments. In this case we want to test a string "abc", so we have passed it as an argument.

The result is printed with console writeline.

This is a brief information about reflection. More can be learnt from the documentation as per needs. Thanks!


This Blog Post/Article "(C# Language) Reflection" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.