Object-to-object mapping is used mostly for copying properties of one object to another, typically for transforming an object from one type to another. There are many use cases. For example, object transformation can help prevent overposting attacks from succeeding by intermediating between APIs and user interfaces exposed to the world and private data models on back-end servers.

You can build your own minimalistic object-to-object mapper with C# and .NET Core without writing a lot of code. This post will show you various approaches, one of which is used in production for an online action game with a massive load.

There are also professional mappers available that come with many useful features the minimalistic code doesn’t provide. This post will show you how to implement AutoMapper, one of the most popular object-to-object mapping libraries.

The mapping techniques demonstrated here were originally developed in 2011. At the time, C# Reflection was even more costly from a performance standpoint than it is today. There was, and still is, a strong motivation to find a faster way of mapping. The approaches demonstrated in this post chart the course of a short technical journey from simple Reflection-based code to dynamic code generation in the search for better performance. As you travel the path yourself you’ll have the opportunity to see the difference in performance between the various approaches and how performance has changed over time.

If you would like to see a full integration of Twilio APIs in a .NET Core application then checkout this free 5-part video series. It's separate from this blog post tutorial but will give you a full run down of many APIs at once.

Understanding the case study project

In the following coding exercise you’ll create four different ways of implementing an object mapper. You’ll also implement the popular AutoMapper library, which provides a suite of highly-optimized object mapping tools. To compare the performance of each approach you’ll create a test that executes each implementation a million times and displays the elapsed time.

Prerequisites

To follow along with the coding exercises presented in this post you’ll need the following tools:

.NET Core SDK (The SDK download includes the .NET CLI and the .NET Runtime.)

Visual Studio 2019, Visual Studio Code, or an IDE or text editor of your choice

Git (If you want to clone the project from GitHub.)

.NET Core is a cross-platform technology: the code in this post will run on Windows, macOS, and Linux.

In addition to the technical resources, you should have a working knowledge of:

C#, including abstract classes, generic types, LINQ, and Reflection

.NET Core, including the LINQ and Reflection APIs

Creating and running .NET Core Console applications with VS 2019, VS Code, or the .NET CLI.

It will also be helpful to have some exposure to the Roslyn code analyzers.

There is a companion repository available on GitHub.

Initializing the case study project

Create a new .NET Core Console application called ObjectToObjectMapper.

Mapping the properties of two objects

The first step in object mapping is to find the matching properties of two objects. You can use the PropertyInfo class of the System.Reflection API, but you can also take a step further and be better prepared by creating a class, PropertyMap , to hold information about the matching properties.

Add a new C# class file to your project root folder named PropertyMap.cs. Replace the existing contents of the class file with the following code: