Reflection in C# with Examples – Programming, Pseudocode Example, C# Programming Example
C#

Reflection in C# with Examples

In C#, reflection is a process of getting metadata of a type that is currently running.

The System.Reflection namespace contains classes that allow you to get information about the application and dynamically add types, values, and objects to the application.

Reflection provides objects (of type Type) describing modules and types. We can use Reflection to dynamically create an instance of a type, link the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If we use attributes in our code, Reflection allows us to access them.

Here is a simple example on Reflection, using the static GetType method inherited from the Object class to get the type of a variable.

Output:

In the following examples, we will use the String class as an example.

Reflection example: Displaying information about a given type

Output:

Example of Reflection: Display the Constructors

Output:

Reflection example: Display the methods available in a given type

Output:

 

Reflection example: Recovering the assembly

Output:

 

 

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: