0

I want to initialize the Expression Call outside the if else condition. As i have to use it to generate expression body because i have two different type coming from database i.e int and int?. My Code is below. I am getting error to instantiate the toString object.

 var parameterExp = Expression.Parameter(typeof(T), "type");
 var propertyExp = Expression.Property(parameterExp, propertyName);
 MethodInfo method = typeof(string).GetMethod(methodType, new[] { typeof(string) });
 var searchValue = Expression.Constant(propertyValue.ToLower(), typeof(string));

var toString = new MethodCallExpression();

if (propertyName == nameof(CustomerListDto.Id))
{
     toString = Expression.Call(propertyExp, typeof(int).GetMethod("ToString", System.Type.EmptyTypes));
}
else
{
    toString = Expression.Call(propertyExp, typeof(int?).GetMethod("ToString", System.Type.EmptyTypes));
}

var body = Expression.Call(toString, method, searchValue);
return Expression.Lambda<Func<T, bool>>(body, parameterExp);

I don't know exactly how to initialize ExpressionCall. This is the thing which i want to know. Currently it is giving me error "MethodCallExpression does not contains constructor that takes 0 argument". I searched a lot but could not find any solution.

7
  • 1
    "I am getting error" - what error? Please provide a minimal reproducible example with the complete results, whether that's a compile-time error or an exception. Commented Oct 3, 2017 at 9:36
  • Could the value of int? just be an null? Commented Oct 3, 2017 at 9:38
  • @FilipCordas. No it contains value. It's datatype is Nullable Int. Commented Oct 3, 2017 at 9:40
  • 2
    You can't instantiate a MethodCallExpression manually as the constructor is private. You can get an instance of MethodCallExpression as the return value of Expression.Call though. You probably just want to declare it like "MethodCallExpression toString;" instead of "var toString = new()..." Then assign it with Expression.Call Commented Oct 4, 2017 at 4:57
  • 1
    @MattSearles. Thanks it helps. I have converted my code with ternary operator like this MethodCallExpression toString = propertyName == nameof(CustomerListDto.Id)? Expression.Call(propertyExp, typeof(int).GetMethod("ToString", System.Type.EmptyTypes)): Expression.Call(propertyExp, typeof(int?).GetMethod("ToString", System.Type.EmptyTypes)); Please give your solution in answer so i can mark it as accepted answer. Commented Oct 4, 2017 at 5:21

1 Answer 1

1

You can't instantiate a MethodCallExpression manually as the constructor is private. You can get an instance of MethodCallExpression as the return value of Expression.Call though. You probably just want to declare it as

MethodCallExpression toString;

// Then assign it with Expression.Call(...);

if (propertyName == nameof(CustomerListDto.Id))
{
     toString = Expression.Call(propertyExp, typeof(int).GetMethod("ToString", System.Type.EmptyTypes));
}
else
{
    toString = Expression.Call(propertyExp, typeof(int?).GetMethod("ToString", System.Type.EmptyTypes));
}
Sign up to request clarification or add additional context in comments.

Comments

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.