Search code examples
c#asp.net-mvclinqlambdaexpression-trees

Evaluate Parameter Result Within Expression<Func<TModel, Object>>


Evaluate the Delegate

I have an expression that I use a parameter with which I want to use the result of within my DoSomething function below.

public void DoSomething <TModel>(Expression<Func<TModel, String>> func){

}

Call the DoSomething Method

The following TModel has a list of names in the Names property which I would like to access from within the DoSomething method. I can't figure out how to do this.

DoSomething(m => m.Names);

Tried to Compile Without Much Success

... 
func.Compile().Invoke([need m.Names]) in here. 

My Code in Reality Looks Something Like This

public static HelperResult TestFor<TModel>(
        ExtendedPageBaseClass<TModel> page,
    Expression<Func<TModel, object>> valueField,
    Expression<Func<TModel, object>> displayField,
    Expression<Func<TModel, ICollection>> list, 
        Object defaultValue = null, String changedEvent = null)   
{
    var idField = valueField.GetName();
    var label = displayField.GetName();
    var display = page.Html.NameFor(displayField).ToString();
    var data = list.Compile().Invoke(page.Html.ViewData.Model);
    return IsolatedSelectorFor(page, idField, display, label, data);
}

Solution

  • You have defined expression which you pass to your method by m => m.Names. However that is only function which, takes as input your TModel and returns list of names. Note that at that point you still did not pass any object. It is just a description of the function.

    So, you need to have an instance of the model in order to pass it to the function and invoke it. Something like this would work:

     public static void DoSomething<TModel>(Expression<Func<TModel, List<string>>> selectNamesFunc, TModel model)
     {
         var f = selectNamesFunc.Compile();
         var names = f.Invoke(model);
     }
    

    Usage of this method would be:

    DoSomething(m => m.Names, modelInstance);
    

    This is only only an example, of how to use, compile and invoke from expression. You need to decide if something like that makes sense and finds application in your domain.