I am trying to use Lambda expression and reflection to get a hierarchical member name (instead of using a text constant) to force compile-time errors if my control binding information is invalid.
This is in an ASP.NET MVC project, but this is not an AFAIK MVC specific issue. EDIT: In particular, I want the following to evaluate to true:
string fullname = GetExpressionText(model => model.Locations.PreferredAreas); "Locations.PreferredAreas" == fullname;
Instead, I get a compilation error:
Error 4: Unable to convert lambda expression to type 'System.Linq.Expressions.LambdaExpression' because it is not a delegate type.
Why does the parameter work in the second case below, but not the first?
Here is the relevant code from the ASP.NET MVC Codeplex project. It seems to me that it passes the same parameter to the same method:
// MVC extension method public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); return TextBoxHelper( htmlHelper, metadata, metadata.Model, ExpressionHelper.GetExpressionText(expression), htmlAttributes); } // MVC utility method public static string GetExpressionText(LambdaExpression expression) { // Split apart the expression string for property/field accessors to create its name // etc...
source share