Get reference to object from c# expression
Asked Answered
V

2

0

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")
Vibratory answered 9/1, 2012 at 19:12 Comment(4)
There is no such object in an expression – x is a parameter of the expression, you're supposed to pass an object of the type into it. (Or I'm understanding what you want to achieve wrongly.)Prostate
Do you really want the object or the text Login to use for the AddModelError(key, errorMessage) method? Use ExpressionHelper.GetExpressionText (built in to MVC) to get the property name from a lambda expression.Christner
@subkamran you probably meant lambda expression...Cohune
I need the reference to TModel object ) I don't want to pass another param with reference to object )Vibratory
E
1

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:

public static void AddError<TModel>(
    this ModelStateDictionary modelState, 
    TModel item,
    Expression<Func<TModel, object>> expression, 
    string resourceKey, 
    string defaultValue)
{
    // TModel's instance is accessible through `item`.
}

Then your calling code would look something like this:

ModelState.AddError<AccountLogOnModel>(
    currentAccountLogOnModel, x => x.Login, "resourceKey", "defaultValue")
Epigene answered 9/1, 2012 at 23:40 Comment(1)
Thanks for reply, I don't see another way also. I done this already yesterday in my code ^)Vibratory
C
0

I imagine you really want the text "Login" to use to add a new model error to the ModelStateDictionary.

public static void AddError<TModel>(this ModelStateDictionary modelState, 
  Expression<Func<TModel, object>> expression, string resourceKey, string defaultValue)
{
    var propName = ExpressionHelper.GetExpressionText(expression);

    modelState.AddModelError(propName, GetResource("resourceKey") ?? defaultValue);
}

Assume you have some resource factory/method that returns null if the resource isn't found, that's just for illustration.

Christner answered 9/1, 2012 at 19:26 Comment(2)
Thanks for reply, but i need to save my error data such as resource key and default value into modelState object and TModel object. With expression i pass property to which error data related, that should be saved into TModel object. )Vibratory
To do that, you'll need to pass in the actual object as a parameter. A generic is not the object reference itself.Christner

© 2022 - 2024 — McMap. All rights reserved.