Microsoft Exam 70–483: Programming in C# — Objective 2.5: Find, execute, and create types at runtime by using reflection

Luis Felipe (LuisDev)
4 min readJul 15, 2019

--

Hello, folks!

In this story, the Objective 2.5: Find, execute, and create types at runtime by using reflection will be discussed! Hope you will enjoy it.

In the last story, the Objective 2.4: Create and implement a class hierarchy was presented.

All the code for the certification series can be found here.

Introduction

Before getting into the topics of the objective, it is worth to spend a short time adding context on it.

The following subjects revolves around metadata. Simply put, metadata is information about data. The .NET Framework enables developers to add metadata related to your code, types and assembly.

Creating and using attributes

Attributes are a way the .NET Frameworks enables developers to add metadata to assembly, methods, types, parameters and properties.

They are popular in different situations/purposes. The following list presents some examples.

  • Serializing: [Serializable], [NonSerializable],[DataContract], [DataMember],[XmlElement]
  • HTTP actions for ASP.NET Controllers: [HttpGet], [HttpPost], [HttpDelete], [HttpPut], [Authorize], [AllowAnnonymous]
  • Testing: [TestClass], [TestMethod] (MSUnit attributes), [TestFixture], [Test] (NUnit attributes), [Fact] (xUnit attribute)

You build your own attributes by creating a class and inherit from Attribute.

Code example:

Using reflection

Reflection enables the developers to retrieve metadata of the code.

It allows you to perform some pretty interesting tasks, such as getting the type of an object, retrieving the list of types on an assembly, displaying information about properties and methods (even the private ones!) of classes and even invoke those methods! Yep, you can become a dll cool hacker.

Code examples:

In the above code, it is shown how to get the type of an object, and go through its properties and methods. Also, a private method is invoked!

Also, information about an assembly and its types is retrieved through reflection.

The ClassForReflectionPractice code, which is instantiated at line 11, is shown next:

Using CodeDOM and Lambda expressions to generate code

What about automating the process of code generation, at runtime? You might be interested in doing that so you can save time when developing for ASP.NET, code wizards, designers, etc. You can realize you are in need of it if you repeat creating the same code several times.

CodeDOM come to the rescue for you. It allows you to create code at runtime by representing the logical structure of code as a graph, by using typical code elements, such as method and property declarations, and using statements. You represent theses code elements by using the CodeCompileUnit, which would be the graph root node, and then adding the other elements by using its specific classes, which are located in the System.CodeDom namespace.

Wouldn’t it be great to be able to create some useful boilerplate for your project?

Code example:

In the above code, it is shown how to create a controller boilerplate for ASP.NET Web API by using the CodeDOM.

Here it is the generated code when passing the value ‘Tasks’ to the console program:

Cool, huh?

Lambda expressions

Before getting into Lambda expressions, it is worth to spend a moment learning about anonymous method.

An anonymous method, as you can understand from its name, is a nameless method, which is of type Action or Func<T,…> , depending whether it returns any value. If you already know TypeScript, you can directly relate Lambda expressions to arrow functions. Lambda expressions are extensively used in LINQ-queries.

Code example:

In the above code, it is shown the power of the numbers from 0 to 10 by using Lambda functions. In the second part, it is using Func<T,…> type in order to create an anonymous method.

Expression Tree

Expression trees are used to represent code in a tree-like structure. As CodeDOM, it can also be used to generate code at runtime.

As with the System.CodeDOM, the System.Linq.Expressions namespace is used to create an expression by offering the types to be used to generate the source code.

Code example:

In the above code, the code for calculating 3 to the power 2 is implemented using Expression Tree!

That’s it for now! I hope that you learned at least something useful whether you are studying focused on clearing the certification exam or just looking for learning more about C#.

See you in the next Story, where I will discuss the Objective 2.6: Manage the object life cycle.

PS: If you find this Story useful, I invite you to hit the Clap button. Also, I would be happy to see you as my new follower!

--

--

Luis Felipe (LuisDev)

3x Microsoft MVP, 9x Microsoft Certified, .NET and Azure Consultant