1

I have below method which gives a Expression<Func<T, Result<T>>> type.

public Expression<Func<T, Result<T>>> GetExpression<T>()
{
    //Do something to retrun expression
}



public class Result<T>
    {
        public bool IsSuccess { get; set; }

        public string Message { get; set; }

        public Result<T> ChildResult { get; set; }
    }

Now I have another method as shown below where I want to access to return result from GetExpression method.

public void UseExpression<T>()
        {
            var expression = GetExpression<T>();

            //I want to get the expression for return Result<T> from above method call and get access to it's properties like IsSuccess which can itself be a binary expression

        }

I want to get the expression for return Result from above method call and get access to it's properties like IsSuccess which can itself be a binary expression. All this without compiling the expression.

End goal: Let's think of it as a method call, method M1 returns Result R1 and method M2 uses this result R1 which(M2) has to create its own result R2 but method M2 has to do an Epxression.Or() on the IsSuccess property of R1 with some other bool expression to create R2 so we need the express. So we need to just get the IsSuccess property from R1 in M2 and then use it.

Please suggest how can I achieve it?

Thanks in advance.

4
  • 1
    If you want to get the result of the execution of an expression, you are gonna have to compile it. Commented Apr 9, 2018 at 10:08
  • 1
    Please try to explain why you want to do that. What is the end goal? Commented Apr 9, 2018 at 10:10
  • yes, at some point, I'll compile the expression but in UseExpression method I'm creating new expression which won't be using compiled expression, so it all has to be expressions and then finally I'll do the compilation. Commented Apr 9, 2018 at 10:10
  • @Daniel update the question. Commented Apr 9, 2018 at 10:14

1 Answer 1

1

but in UseExpression method I'm creating new expression which won't be using compiled expression, so it all has to be expressions

There are two ways to do this.

The first way is to take the expression.Body which gives you the inner expression tree, and either re-use the expression.Parameters to build your new expression (assuming it still takes the same parameter types), or use an ExpressionVisitor to replace the parameters throughout the current .Body with whatever it should now be. If you have a concrete example of what you're trying to do, I could illustrate this with a complete example.

The second way is to use .Invoke in your new expression, passing the entire expression as the first parameter, and supplying whatever your new expression wants to use as the parameters as the parameters. However, this is slightly less reliable if it is going to be passed to something like EF, and it may decide that it isn't supported. If you're going to call .Compile you should be fine, though.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. In my case I need to get the access to IsSuccess property of Result class under UseExpression method. Invoking just the expression returned by GetExpression method will give a complex type, I want to access it's properties.
@user7784348 do you have a specific example we could work through here? it is very hard to answer these things without a specific example. Could you add some dummy code into "Do something to retrun expression", and give an example of what UseExpression would want to do with it, for example?
I've created a sample proj and uploaded to this link . Please let me know if you have any query. Thanks in advance.
@user7784348 perhaps something like this? gist.github.com/mgravell/26a9ee17d720e5fda7c1e005fba4e70c - here I've basically explored the tree to resolve the IsSuccess assignment, used AndAlso to combine them, then built a new binding with that combined result. But there are other ways to do the same thing
Thanks a lot. It works perfectly! Appreciate your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.