I'm trying to write a strongly typed helper which would be something like this:
Html.Lookup(x => x.FooId);
for now I have this:
public static MvcHtmlString Lookup<T,TReturn>(this HtmlHelper<T> html, Func<T, TReturn> expression)
{
// get string "FooId" here
}
Anybody knows how to get this ?
Currently using this class when I need this functionality outside of web project where
System.Web.Mvc
reference shouldn't exist:Good thing about this one is - it does not lose dots when going deeper than just one level.
Yet another code.
Use ExpressionHelper class. Func is delegate, Expression is generate ExpressionTree at compile time. Expression.Compile() return delegate, but Func don't get ExpressionTree at runtime.
You would then call it with:
a bit late but I am posting a simple solution that's working for me in .Net 4. It has handling for value types on line 4