Reflection in .NET – Part I ( Tutorial Series)

Preface

In these series of articles we would discussing in detail on Reflection in .NET and then take a deep dive into different aspects of Reflection and cover the various classes in System.Reflection namespace. On our path to understanding Reflection, I have tried to include sample examples (demo programs) to explain the concept practically. I will include various techniques using examples (demo programs) which will be help for programmers to use them in their applications while implementing Reflection.

Introduction

An Assembly is the minimum unit of deployment. You cannot deploy anything less than an assembly. For the CLR, a type does not exist outside of the context of an assembly. Assemblies contains four elements (The assembly manifest, Type metadata, MSIL and resources.). Each assembly contains a collection of data that describes how the elements in the assembly relate to each other. For more information on Assemblies (see bottom section – References). We can use this Type metadata information in our custom .NET application with the use of Reflection

What is Reflection ?

Reflection in .NET allows us to programmatically load the assemblies and obtain information about the types (classes, interface and value types) defined in the assemblies
Reflection can also be used to dynamically create instance of a type, bind the type to an existing object, or get the type information from and existing object. The .NET framework includes, System.Reflection and the System.Type and they together form the reflection API, this API can be used to programmatically obtain information the loaded assembly.

Reflection also allows you to perform late binding – where in you load the assembly, access the type, create an instance of the type, invoke the methods of the type (using the method name) all at runtime.

Let’s start with our first Demo:

Using Reflection for Loading a .NET framework assembly and displaying the types.

Below is the complete running code. I will walk you through each line of the code.

Reflection_Image1

In this console application example – we are loading the .Net assembly (System.Data.dll) and looping through the types contained in this assembly at runtime using the reflection.

First you specify the reflection namespace using the using directive.

using System.Reflection; // Using the Reflection Assembly.

Under the Main method (entry point for c# console application) define string variable to specify the exact location (path) of the assembly. (You can specify any other .NET assembly name with path).

string assemblyPath = @”C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\System.Data.dll”;

In the next line, the above specified path would be used to load the assembly using LoadFrom which accepts a string parameter (path of the assembly)

Assembly assembly = Assembly.LoadFrom(assemblyPath);

In the next line we display the full Name (fully qualified Name of the Assembly with Namespace information, name, version, culture and public key) of the assembly that’s loaded.

// Get the complete name of the assembly that is loaded from file using FullName or GetName()
Console.WriteLine(“Assembly Full Name : {0}”, assembly.FullName);

In the next line using the GetTypes() method we will get all the types from the loaded assembly in a Type[ ]

//Store the types contained in assembly in array
Type[] assemblyTypes = assembly.GetTypes();

We will then use a foreach to loop through the types and display the Fullname of each type on console screen.

foreach (Type t in assemblyTypes)
Console.WriteLine(t.FullName);

Reflection_Image2

We can further extend this example, to display the methods and public properties contained used each type for this loaded assembly.

Below is the complete running code. I will walk you through each line of the code.

Reflection_Image3

Inside the foreach loop we define an array of memberInfo and then using GetMembers() method of Type we get the member information.

Note: Here I have specified the BindingsFlag – it’s used for specifying the way in which reflection should do search for the Member under each type (in this case the search should be one – only public member, second – member which are declared in the Type and not inherited from base type and third search for only instance members i.e. non-static members).

foreach (Type t in assemblyTypes)
Console.WriteLine(t.FullName);

We then display the member type and the member name under each Type for the loaded assembly.

Reflection_Image4

In the Next article we will load a custom .NET application Class library dll and find the type, and member information. We will discuss in detail the System.Type

Happy Learning.

Husain Patel

 

Leave a comment