Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make String Interpolation work with dictionary

Tags:

string

c#

.net

I have a dictionary with key value pair, and I need to use String Interpolation and then map the string according to the named parameters. Please help me.

// code removed for brevity
var rawmessage = "Hello my name is {Name}"; // raw message
var dict= new Dictionary<string, object>();
dict.Add("{Name}", response.Name);

I want to be able to do something like:

// Pseudo code
$"Hello my name is {Key}", dict;

Solution I have found till now:

     var message = parameters.Aggregate(message, 
(current, parameter) => current.Replace(parameter.Key, parameter.Value.ToString()));

And it works fine, but my manager really wants me to use String Interpolation with Dictionary. I am not sure how to achieve this. Please guide me.

like image 231
Unbreakable Avatar asked Dec 06 '22 11:12

Unbreakable


1 Answers

The problem is that string interpolation in C# is just a syntactic sugar wrapper for string.Format. The strings get compiled to the same code. Take this code for example:

public string GetGreeting1(string name)
{
    return string.Format("Hello {0}!", name);
}

public string GetGreeting2(string name)
{
    return $"Hello {name}!";
}

The IL output for both methods is identical:

GetGreeting1:
IL_0000:  ldstr       "Hello {0}!"
IL_0005:  ldarg.1     
IL_0006:  call        System.String.Format
IL_000B:  ret         

GetGreeting2:
IL_0000:  ldstr       "Hello {0}!"
IL_0005:  ldarg.1     
IL_0006:  call        System.String.Format
IL_000B:  ret  

So the solution you have is perfectly valid and one I've used before. The only possible improvement would be if you intend on doing lots of replacements, you may find a performance benefit if you switch to using a StringBuilder. You could probably find a library on Nuget that does it for you, but that might just be overkill for your purpose.

As an extra, I made an extension class for this. With an extra bonus I've also added a method for using an object with any number of parameters that uses reflection to do the replacements:

public static class StringReplacementExtensions
{
    public static string Replace(this string source, Dictionary<string, string> values)
    {
        return values.Aggregate(
            source,
            (current, parameter) => current
                .Replace($"{{{parameter.Key}}}", parameter.Value));
    }

    public static string Replace(this string source, object values)
    {
        var properties = values.GetType().GetProperties();

        foreach (var property in properties)
        {
            source = source.Replace(
                $"{{{property.Name}}}", 
                property.GetValue(values).ToString());
        }

        return source;
    }
}

And use like this:

var source = "Hello {name}!";

//Using Dictionary:
var dict = new Dictionary<string, string> { { "name", "David" } };
var greeting1 = source.Replace(dict);
Console.WriteLine(greeting1);

//Using an anonymous object:
var greeting2 = source.Replace(new { name = "David" });
Console.WriteLine(greeting2);
like image 152
DavidG Avatar answered Dec 14 '22 23:12

DavidG