Asp.net MVC introduced the EditorFor
method on the Html generic class. It allows you to write code that succinctly identifies a field of the view model. In this case the page this code is from must have a view model of some type that has a StudentId
property or else this won't work.
@Html.EditorFor(m => m.StudentId)
The signature of the EditorFor
property is something like this.
EditorFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>)
The method is defined on a generic type that knows the type of the TModel
. So the lambda expression can be as simple as m => m.StudentId
and we all know that the type of m
is whatever TModel
is. In this case it is the page's view model.
I would like to be able to write similar code, without knowing what type the property is defined on. I'd like to be able to write...
@Html.EditorFor(M.StudentId) // here M is a type not a lambda parameter
@Html.EditorFor(X.Y) // here X is a type not a lambda parameter
I'd like to be able to specify an arbitry type, and an arbitrary parameter, and have the method called with something that identifies both. For example if the method were called with a PropertyInfo
then I could see both the property and the type it was defined on, both.
Said another way... In the same way that nameof(X.Y)
gives a string "Y"
for any arbitrary type X
, I'd like an expression that gives something like a PropertyInfo
too. Maybe property(X.Y)
and you get back the PropertyInfo
of the property Y
from the type X
.