Coding Concepts - Reflection
Tags: webdev, oop, learning, csharp
Published: 2018-07-05 13:38:55

---
title: Coding Concepts - Reflection
published: true
description: Reflection, What you need to know.
tags: webdev, oop, learning, csharp
cover_image: https://thepracticaldev.s3.amazonaws.com/i/swgf0o6hfnuepuxt5dvv.png
series: Coding Concepts
---

<h1>Reflection - What you need to know.</h1>
<blockquote>In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.[1]</blockquote>
So what does that mean? Reflection is a term thrown around every now and again and but do you really know what it means? Why and when it should be used, and what are its main strengths? It's a pretty difficult concept to grasp, but it's well worth the effort to learn as it can make certain seemingly impossible tasks possible.

Usually I run through examples in JavaScript to keep it understandable for most people, however reflection in JavaScript is not really the same as  in precompiled Object Oriented languages such as Java and C#, as it doesn't contain classes per se, so we'll be using examples from those today. The .Net Framework and Common Language Runtime (CLR) use reflection heavily for features such as Intellisense and other IDE features within Visual Studio as well as for serialisation so you'll probably be using reflection without even knowing about it.

Let's take a look at this example in C#.

``` csharp
// Using GetType to obtain type information:  
int i = 42;  
System.Type type = i.GetType();  
System.Console.WriteLine(type);
```

As you would have guessed the output in the console is:
<pre>System.Int32</pre>
So this static method <strong>GetType</strong> uses reflection to obtain the type of the variable! Obviously when writing the code we know that "i" is an Integer but the runtime doesn't have this knowledge to hand, so it must search within itself to figure it out! Most people will have used <strong>GetType</strong> in the past, but some may have not known how it obtains the type!

Let's look at the main uses of Reflection and answer some of the questions raised above, I'll try and keep it easy to follow.
<h2>When to use reflection and why!</h2>
<ul>
 	<li>Traditionally used to load modules/classes from assembly and create an instance of them, at runtime.</li>
 	<li>For getting an Object's Public Attributes.</li>
 	<li>During testing, creating mock objects during runtime initialisation.</li>
 	<li>To create Generic libraries to handle different formats without redeployments, sometimes referred to, or using Implicit Late Binding.</li>
 	<li>When building new types at runtime.</li>
 	<li>For examining and instantiating types in an assembly.</li>
 	<li>The ability to change the value of a field marked private in a 3rd party library.</li>
</ul>
<blockquote>The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++</blockquote>
So as you can see it's mainly about classes, looking at them, changing them, instantiating them, but all at runtime rather than compile time!

<img class="wp-image-1047 size-large" src="http://blog.designpuddle.com/wp-content/uploads/2018/07/CompileVsRun-1024x414.png" alt="Compile Time Vs Runtime" width="1000" height="404" />

The best way I can explain the difference between the two is by looking at your running shoes!

<strong>Compile time</strong> is like doing up your shoe laces and checking them before you go out for a run. Whatever you do to your shoes will be saved and kept. This will give you an insight whether anything is wrong with them, and what they contain and can do.

<strong>Runtime</strong> is when you're already out on the road! Your shoes have already been prepared for you, you can't usually change those shoes or retie those laces unless you stop, but you can see what the shoes are capable of, and use any existing features that these shoes have available! <span style="font-size: 1.8rem;">You could switch shoes by jumping into a new set, but this requires the new set of shoes to be already created too and available to you on the run.</span>
<blockquote>In object-oriented <b>programming</b> languages, <b>reflection</b> allows inspection of classes, interfaces, fields and methods at <b>runtime</b> without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.</blockquote>
So above we had an example of using reflection on an object and getting the object attributes, now lets looks at using reflection to get info from a Type, and how we'd find and load new assemblies during runtime.

``` csharp
		// Using Reflection to get information from an Assembly:  
		System.Reflection.Assembly integerTypeAssembly = typeof(System.Int32).Assembly;  
		System.Type integerType = typeof(System.Int32);
		
		System.Console.WriteLine(integerTypeAssembly); // mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
		System.Console.WriteLine(integerType.ToString()); // System.Int32
		
		//Use this typeString to refind the type
		Type calcType = integerTypeAssembly.GetType(integerType.ToString());
		System.Console.WriteLine(calcType); // System.Int32
		
		// We can then Create a new Instance of whatever this type may be, and call it's methods.
		object integerInstance = Activator.CreateInstance(calcType); 
		// In this case it's just a boring Integer, and we can see it's been initialised to it's default of 0..
		System.Console.WriteLine(integerInstance.ToString());
```

We can see the nuanced difference of looking at a variable that was set during compilation, and querying the library itself, and the different ways we can use these two types of reflection to make changes to the system during runtime.
<h2>A Real World Example</h2>
A useful real world example of using reflection would be switching between assemblies based on a value stored in a database or other static resource. For example: you have "GoogleSearchService" and "BingSearchService" configured in your system. <strong>Usually</strong> you would hard code/inject one of these services as the one to be used.

But say for instance you had a "LoadSearchServiceAssemblyFromResource()" method that retrieved the service from your database which would then be used at runtime.

If this service was to go down, become obsolete, or your contract was to be terminated, rather than having to redeploy your system using "BingSearchService", you would simply update your database/storage field pointing to your other assembly("BingSearchServiceAssembly").

The next time your Search Service was called, it would switch dynamically at runtime to your new Service. This for me is the  major benefit of reflection and why it can or should be used. As each service is a dll you can call them directly using reflection using a format such as this.

``` csharp
//This will load the dll from a static resource such as a DB or File
Assembly searchAssembly = LoadSearchServiceAssemblyFromResource();
// Now we need to find the class in the assembly, this in this instance is simply called "Search"
Type searchType = searchAssembly.GetType(searchAssembly.GetName().Name + "Search");

// If we have found the class
if (searchType != null)
{
	object searchResult = null;
	dynamic classInstance = Activator.CreateInstance(searchType);
	// DoSearch is a method that is declared on both SearchService Classes!
	var searchResult = instance.DoSearch();
}
```

You may not always have the scenario to use it, but knowing when and why you should is invaluable.

There's load more to Reflection that hasn't been covered here. So have a look at the additional reading if you want to read more on the topic.

<hr />

<h2>Additional Reading</h2>
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection" target="_blank" rel="noopener">Microsoft Concept Docs - Reflection</a>

<a href="https://thesocietea.org/2016/02/programming-concepts-type-introspection-and-reflection/" target="_blank" rel="noopener">Type Introspection and Reflection</a>

<a href="https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/reflection" target="_blank" rel="noopener">Microsoft - Reflections and CodeDom</a>

<a href="https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/dynamically-loading-and-using-types" target="_blank" rel="noopener">Microsoft Reflection - Dynamically Loading and Using Types</a>

<a href="https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful" target="_blank" rel="noopener">StackOverflow - What is reflection and why is it useful</a>