jeudi 15 septembre 2016

MVC DisplayFor using reflection

I have a HtmlHelper which receives a List(of TModel) as a parameter

I'm using reflection to iterate all properties of TModel and create a MvcHtmlString for each of these properties using HtmlHelper.DisplayFor

I create the expression for the DisplayFor using this code

Dim expressionParameter = Expression.Parameter(GetType(TModel), "PropertyName")
Dim expressionProperty = Expression.PropertyOrField(Expression.Constant({Object}), "PropertyName")
Dim expressionLambda = Expression.Lambda(Of Func(Of TModel, Boolean))(expressionProperty, expressionParameter)

helper.DisplayFor(expressionLambda, "TemplateName")

Problem is the "Boolean" in Func(Of TModel, Boolean), this only works of course if the actual property on TModel is of type Boolean.

How can I make Func(Of TModel, Boolean) more generic so I can use the PropertyType of the property?





Aucun commentaire:

Enregistrer un commentaire