Torben Nielsen
Torben Nielsen

Reputation: 833

Using DynamicExpression.ParseLambda with string interpolation

I am struggling with DynamicExpression.ParseLambda and string-interpolation

The following ParseLambda fails on the $ character. Can anyone see why?

private static void Main()
{
    var result = ExecuteJob("parameter.Trim()", "    TheVale");
    Console.WriteLine(result);
}

static string ExecuteJob(string job, string parameter)
{
    //var result = new Func<string, string>(j => $"{parameter.Trim()}");
    //return result(parameter);

    var expression = $"$\"{{{job}}}\"";
    var p = Expression.Parameter(typeof(string), nameof(parameter));
    var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p }, typeof(string), expression);
    return (e.Compile().DynamicInvoke(parameter) ?? "").ToString();
}

Upvotes: 0

Views: 447

Answers (0)

Related Questions