I have an extension generic method
public static void AddError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression,
string resourceKey,
string defaultValue)
{
// How can I get a reference to TModel object from expression here?
}
I need to get the reference to TModel object from expression.
This method called by the following code:
ModelState.AddError<AccountLogOnModel>(
x => x.Login, "resourceKey", "defaultValue")
You cannot get to the TModel object itself without passing it into the method. The expression you are passing in is only saying “take this property from a TModel”. It isn’t actually providing a TModel to operate on. So, I would refactor the code to something like this:
Then your calling code would look something like this: