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; } } 
                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.
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.
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.
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.
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" 
                        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); } 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With