Challenge #2 — Binding object to string — Deep Dive into Reflection

Enis Necipoğlu
4 min readMay 7, 2020

--

Hi again,

This time, challenge is a simple problem which I thought at my daily routine. The idea comes from trying to pass and place some parameters into e-mail templates. There was no problem with plain text templates, but when try to use html templates, it has started to be harder and complicated to manage. So I needed basically to bind each object properties into placeholders in html string.

I’ll start with sharing some method to bind parameters to strings in .Net environment. But I need a different way which is easy to use with less lines of code.

Photo by Glenn Carstens-Peters on Unsplash

— -

Let’s start with a sample. I need exactly same thing with following usage. There is a raw string template with placeholders, then I want to be able to bind any type of object to string. Each object properties must be replaced with own placeholder. Let me show with some code:

This one looks too simple and can be made any existing method in C#. But let’s make it a little bit complicated and bind object properties into html data:

Now, my approach is more meaningful. So template string can be much much more complicated and can be read from a file or database. Maybe it can be an e-mail template or something else. This is the challenge: “Binding object proeprties to string!”

— -

There is a couple of ways to do that in Csharp.

So let’s start with a classical way:

Concatenate String Using + Operator

This is really primitive way to build a string but it works well. See following example:

String Interpolation

This one is awesome feature of C# and I love using it. String Interpolation is available since C# 6.0. Just place a ‘$’ operator at the beginning of string and use any variable between curly braces. It’s Amazing!

String Format

This one is most common approach to place parameters into strings. Use some placeholders into string then give them one by one Format method.

There are much more approach to bind parameters into a string and get one single output. Most advanced approach is made by Microsoft and its name is Razor Engine. I think each .NET Developer knows that. It’s part of Asp.NET MVC and provides to generate HTML outputs with parameters which comes from C# models. This one is awesome, but too heavy to use my dynamic bindings. I needed to bind each object property into a string. For example I have a User object and it includes 16 properties. I don’t want to use + (plus) operator for 16 times to concatenate entire string. Also string format is almost same… I don’t want to write each 16 properties as method parameter. It’s unmanageable. Object properties can be changed, added or be deleted.

Before starting, we must know something like: how to find and iterate object properties at runtime and replace them with its placeholders. Let’s deep dive into Reflection.

Reflection in .NET

System.Reflection is a namespace which provides to explore Types and members in compiled assemblies. Visit here for much more details.

We start with getting object properties with reflection. Just realize something, before diving into reflection.

How to reach object properties in normal ways?”

Member Access Operator (.)

Everyday, each developer uses dot (.) operator and mostly even don’t know it’s an operator to access a member of an object. When you use user.Email , you access Email member of object which placed in user named variable.

Let’s try to make same operation with reflection:

As you can see, property name can be used as string. This makes me free to find any property name in any type of object.

Iterate object properties

Iterating object property names and values is easy and can be done with one single foreach statement.

As you can see, I can access each property name and values as dynamically. That is an awesome knowledge to complete my challenge. I’ll find property names between curly braces and replace them.

— -

Binding Object Properties into Placeholders in String

This is most awesome part of article. Let’s use following string template which includes some property names in it.

Welcome back {Name}. You're authenticated as {Email}. Have a good day.

I can find each object property names and replace them easily.

Yeah, this one works. But it’s awful code. It is not performing. This is object-first approach and tries to replace even property placeholder doesn’t exist in string template. Let’s think about approaching template-first instead of object-first. To make that, I need to find all placeholders in string template. Following method helps me to find all placeholders in string.

Then I can iterate this IEnumerable

That one works but it’s not perfectly yet. It works via accessing directly to object members.

❗ But will not work for nested object members with trying to access with multiple dots like following string. 🤔

Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day.

— -

I need to split each parameter name with ‘.’ (dot) and try to access each nested members to find last one. So, following method helps to do that:

Now I can get nested object property values. Let’s complete binding method:

— -

Now works perfectly 👌

You can see and use entire BindingExtensions class:

Alternatives

Thank you four reading until here. :)

This approach has alternatives to use. Most common one is Razor as I told before in article. If you know and use another alternatives you can inform me.

Have a good day :)

--

--

No responses yet