Expression trees represent code in a tree-like data structure where each node is an expression, for example, a method call or a binary operation such as x < y.
Using the above feature, we can retrieve the property name of class:
To retreive the name of property: "
" of class, we build the following expression:
Country c = new Country();
Expression<Func<string>> exp =() => c.ContryName;
string propName = exp.Body.ToString().Substring(exp.Body.ToString().LastIndexOf('.') + 1);
Use of the
in this case is redundant, because we need to know the property name to call the method.
Please let me know if it is useful.