I'm working on something similar to a text templating engine.
I'm providing metadata from my server to the client to represent an javascript version of the access path for instance:
Say I have a DTO:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have a mapping on the server which looks like so:
Expression<Func<Employee, string>> firstNameExpression = e => employee.FirstName;
When returned to the client I would like to return something similar a string representation of the expression
$"{nameof(Employee)}.{nameof(Employee.FirstName)}";
I would prefer not to have to parse the expression manually or walk the expression tree.
//e.g Pseudo Code
LambdaExpression expression
if(expression is MemberExpression expr)
{
stringBuilder.Prepend(expr.Body.Member.Name)
}
//... Handle errors
Is there a simple way to output and expression as if it were written in code in some way?