c sharp

C# reflection

As the name suggests, the reflection is something that gives an information image about something. Within C#, a reflection is a well-known namespace found in the “System” library to see metadata about the classes, methods, parameters, or types. It contains a lot of classes and attributes to get different information regarding the code aspects. So, we have decided to perform some very simple examples to elaborate on the use of reflection namespace along with its attributes. Let’s get started with the C# file creation.

Example # 01:

Getting started with the first example, we will be looking at the elaboration of the use of the “System.Reflection” class to find metadata information regarding different types, methods, or fields. So, we have started this code with the use of the System library, and after that, we have utilized the “System.Reflection” class in the code. We have started a namespace Test and a class Test within it.

This class Test contains a single function, “Main()” which is going to be our driver function for this code. The main() function has been started with the keyword “Type,” loading a type “t1” of a string via the “typeof()” function from the System class. The “Type” came up from the Reflection class. Now, the same procedure has been used to load the type of t2 and t3 of an integer and character, respectively. As we know that the Reflection class came up with many of the metadata attributes to find out the information regarding types, methods, and fields.

So, we will use those metadata attributes here to find information regarding types t1, t2, and t3. Thus, we have used three Console.WriteLine() function statements to display the Name, FullName, Namespace and BaseType of type t1, t2, and t3 separately. For this, we have been utilizing the “Name,” “FullName,” “Namespace,” and “BaseType” attributes of a reflection class here in the code.

We have been compiling this newly made “reflection.cs” file with the C# compiler “mcs” already built-in in our Linux system. The “exe” file for this code has been generated, and we have used it to run the compiled code with the “mono” runtime command. The output displayed on our screen shows us the Name, FullName, Namespace, and BaseType information for the loaded type string, integer, and character.

Example # 02:

In our first example, we got information regarding Type in C# metadata using the Reflection class attributes. We will be using the Reflection class to get metadata information regarding the loaded “Assembly.” So, we have been starting our example with the use of the “System” library in the first line and using the “System.Reflection” class in the second line.

This code contains a namespace Test with the same name class Test. In this class Test, we have been using the Main() driver function to start executing our system. Within the main() function, execution starts from loading an assembly “t.” This loaded assembly variable “t” will be getting the metadata information regarding the “int” assembly of C# via the “typeof” function using “int” within it, followed by the dot and “Assembly” keyword. The next exact line has the Console.WriteLine() function to display the metadata information regarding the assembly “t.” The class and namespace are completed here, and the code is saved for use.

We have compiled this newly made C# code with the “mcs” compiler and then executed it with the “mono” runtime executor for C# in Ubuntu 20.04. It displayed the name of a library for “t” assembly as “mscorlib,” its version, culture, and public key token on our screen beneath.

Example # 03:

Within our last example, we will be taking a look at the metadata information for all the classes, methods, attributes, or parameters used in the code via the Reflection class. So, we have been starting this code with the same code pattern as we have used in the above two examples, i.e., the use of the System library and the “System.Reflection” class. A namespace “New” has been created that holds two classes in it, i.e., Info and Test.

Let’s discuss the “Info” class first. Within this class, we have been using defining the properties or variables we have to use in this code, i.e., Age of int type and Name of string type. The “get” and “set” attributes are utilized to get the value for both values and assign them to the variables Age and Name, respectively. Here are two constructor functions in the class Info. The first constructor has no parameters, while the other is a parameterized constructor getting values in new variables “a” and “n.” The simple constructor initializes the values of variable Age and Name to 25 and “Empty,” respectively.

On the other hand, the other constructor has been assigning random variable values to Age and Name variables, i.e., empty. The user-defined “show()” function has been used here to display the variable ‘Age’ and ‘Name’ values on the Console of Ubuntu 20.04 using the WriteLine() function from the Console class of the System library. The first class has been completed here.

Let’s discuss the “Test” class where we have been utilizing the “Reflection” attributes to get metadata information regarding the code. So, it contains a main() method that is our executor for this code. We have loaded an object for class assembly “Asb” to get the currently executing assembly with the ‘GetExecutingAssembly’ function. This object “Asb” has been used to call the “GetTypes()” function to store the assembly type information within the Type Array “t.”

Now, it’s time to iterate each type from the assembly and get information regarding its metadata. So, within the first “foreach” loop, we have been iterating the type used in the code via the Type array “t,” i.e., classes. The name of the total classes used will be displayed by the “Name” attribute of the Reflection class. An array “Met” of MethodInfo type has been initialized to get total methods in the specific class using the iterator “I.” An inner “foreach” loop is used to display the name of each method within the specific class via the Name attribute.

The ParameterInfo type array “Par” is defined to get the parameter information for all the methods in the specific class using the “GetParameters()” function. The last inner loop is used to iterate all the parameters “p” found in the “Par” array to display the Parameter name and ParameterType at the shell using the Name and ParameterType attribute of the Reflection class. Our code is now complete and ready for compilation.

The metadata information regarding “Info” and “Test” classes is displayed separately. The classes have parameter int type methods; string type methods; and Object type methods


This is how a Reflection namespace class can be utilized for getting metadata information regarding methods, types, parameters, assemblies, and classes. Within our first two examples, we have discussed the use of Name, FullName, Namespace, BaseType attribute of the reflection class to get information regarding the loaded types and assembly, i.e., int, char, string. In the last example, we have used the GetTypes, GetMethods(), GetParameters, and the name and Parameter type attribute of the Reflection class to get metadata about the whole code.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.