0

I want to use string interpolation with a template, but I can't get it working. I have the following code:

public string Parse(string template, params dynamic[] objects) { return $"text text {objects[0].PropertyOne} {objects[0].PropertyThree} text text"; } 

This works fine, but I want to use the string template passed into the method, so it seems I need something like this:

public string Parse(string template, params dynamic[] objects) { return string.Format(template, objects[0].PropertyOne, objects[0].PropertyThree); } 

But I don't know how many parameters will be passed, or with which properties (hence the dynamic type). I need something that would work like this:

public string Parse(string template, params dynamic[] objects) { // template = "text text {objects[0].PropertyOne} {objects[0].PropertyThree} text text" return string.Format(template, objects); } 

Everywhere I look it says string interpolation is just an abstraction of string.Format, or that it uses string.Format in the end, so it sounds like something like this should be possible. Can I do what I aim for without reflection?

3
  • String.Format needs numerical placeholders ({0} and such), you cannot specify variables or expressions Commented Dec 4, 2021 at 15:50
  • 1
    I don't understand why you're trying to write a method that takes a formatting template and N params; that's what string.Format does. For your code to then be a one liner that returns string.Format of the input arguments makes it especially pointless - Instead of calling your Parse() (which appears to be a wildly incorrect name, btw; this is the opposite of parsing) method, the calling site should just call string.Format itself, or use an interpolated string on the variables it has locally Commented Dec 5, 2021 at 1:00
  • 1
    If your method has some value to add, then you should look at FormattableString, but be sure you actually have a use case for it. For example Entity Framework makes good use of formattable strings when it does a raw SQL execution; it is important for sql injection prevention to get access to the placeholdered string before it is formatted so that it can be made into a parameterized statement Commented Dec 5, 2021 at 1:03

1 Answer 1

1

You cannot make it dynamic as you have mentioned in your question.

You can do like this

// template = "text text {0} {1} text text" String.Format(template, objects[0].PropertyOne, objects[0].PropertyThree); 

Here is the official documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

And this is a problem for me, since I don't know how many placeholders the template will have, or which properties should be used.
That’s why I said you can’t do that, you need to write code for that to define how do you want to get the property of object.
@rock_crusheria you issue resolved?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.