Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic string interpolation

Can anyone help me with this?

Required Output: "Todo job for admin"

class Program {     static void Main(string[] args)     {         Console.WriteLine(ReplaceMacro("{job.Name} job for admin", new Job { Id = 1, Name = "Todo", Description="Nothing" }));         Console.ReadLine();     }      static string ReplaceMacro(string value, Job job)     {         return value; //Output should be "Todo job for admin"     } }  class Job {     public int Id { get; set; }     public string Name { get; set; }     public string Description { get; set; } } 
like image 236
Jitendra Tiwari Avatar asked Oct 05 '16 12:10

Jitendra Tiwari


People also ask

What is meant by string interpolation?

An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results.

What is the syntax for string interpolation?

Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.

What is string interpolation in Dart?

String interpolation is the process of inserting variable values into placeholders in a string literal. To concatenate strings in Dart, we can utilize string interpolation. We use the ${} symbol to implement string interpolation in your code.

What is interpolation in JS?

String interpolation in JavaScript is a process in which an expression is inserted or placed in the string. To insert or embed this expression into the string a template literal is used. By using string interpolation in JavaScript, values like variables and mathematical expressions and calculations can also be added.


2 Answers

Two suggestions:

DataBinder.Eval

string ReplaceMacro(string value, Job job) {     return Regex.Replace(value, @"{(?<exp>[^}]+)}", match => {         return (System.Web.UI.DataBinder.Eval(new { Job = job }, match.Groups["exp"].Value) ?? "").ToString();     }); } 

Linq.Expression

Use the Dynamic Query class provided in the MSDN LINQSamples:

string ReplaceMacro(string value, Job job) {     return Regex.Replace(value, @"{(?<exp>[^}]+)}", match => {         var p = Expression.Parameter(typeof(Job), "job");         var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p }, null, match.Groups["exp"].Value);         return (e.Compile().DynamicInvoke(job) ?? "").ToString();     }); } 

In my opinion, the Linq.Expression is more powerful, so if you trust the input string, you can do more interesting things, i.e.:

value = "{job.Name.ToUpper()} job for admin" return = "TODO job for admin" 
like image 196
Dan Avatar answered Oct 06 '22 09:10

Dan


You can't use string interpolation this way. But you can still use the pre-C#6 way to do it using string.Format:

static void Main(string[] args) {     Console.WriteLine(ReplaceMacro("{0} job for admin", new Job { Id = 1, Name = "Todo", Description = "Nothing" }));     Console.ReadLine(); }  static string ReplaceMacro(string value, Job job) {     return string.Format(value, job.Name); } 
like image 20
Jonesopolis Avatar answered Oct 06 '22 07:10

Jonesopolis