Library that matches the interpolation string of C # 6.0 at runtime

2

Does anyone know of any library that does run-time interpolation in strings?

I know it would be possible to do using Replace() , but I do not want to go through the list with all the variables.

For example:

Programa de classe
{
    const string interpolados = $ "{FirstName}"; 
    // variável *FirstName* não existe no contexto atual

     static void Main (string [] args)
     {
        var firstName = "fred";
        Console.WriteLine (interpolado);
        Console.ReadKey ();
     }
}

interpolados can also be a text with several variables.

How to do something similar?

    
asked by anonymous 22.06.2016 / 15:25

1 answer

3

No library is required, .Net has always had this feature. In fact the interpolation shown is new and few know it. This is the first time I meet someone who knows the new feature, not the old one.

It may not work well with the syntax you want, but this syntax to resolve at runtime is bad and error-inducing and matches names that the code can not always guarantee.

It's technically possible to make a library that accepts variable names instead of parameter numbers, but I do not see any advantages.

Note that the example shown can not do anything because you would have to pass the parameter at run-time. In fact not even using C # 6 interpolation this code works.

What you want is what you do with string.Format() . The same one that has always been used internally in a WriteLine() , for example.

var texto = string.Format("{0}", firstname);

See running on dotNetFiddle and on CodingGround .

C # 6 interpolation is a compile-time mechanism. I 'm talking about it in the question What does the "$" symbol mean before a string?

I'll quote a few libraries, but think 10 times before adopting them, it may seem like a good thing, but it's not what you need. I do not know them, I can not speak of their quality:

It has a code in CR.SE that can be an inspiration to make the mechanism itself (it is not easy think of everything).

    
22.06.2016 / 15:38