How should I construct Expression tree for string.IndexOf("substring", StringComparison.OrdinalIgnoreCase)
?
I can get it working without the second argument: StringComparison.OrdinalIgnoreCase
. These are my attempts so far:
var methodCall = typeof (string).GetMethod("IndexOf", new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Constant("StringComparison.OrdinalIgnoreCase", typeof (Enum))};
var exp = Expression.Call(left, methodCall, parms);
return exp;
Also tried this:
var methodCall = typeof (string).GetMethod(method, new[] {typeof (string)});
Expression[] parms = new Expression[]{right, Expression.Parameter(typeof(Enum) , "StringComparison.OrdinalIgnoreCase")};
var exp = Expression.Call(left, methodCall, parms);
return exp;
Please remember that I can get it working if I ignore the OrdinalIgnoreCase
parameter.
Thanks
The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y . You can compile and run code represented by expression trees.
There are four variants of indexOf() method.
An expression in C# is a combination of operands (variables, literals, method calls) and operators that can be evaluated to a single value. To be precise, an expression must have at least one operand but may not have any operator.
I suspect there are two problems.
The first is the way you're getting the method - you're asking for a method with only a single string parameter, instead of one with two parameters:
var methodCall = typeof (string).GetMethod("IndexOf",
new[] { typeof (string), typeof(StringComparison) });
The second is the value you're giving - it should be the actual value of the constant, not a string:
Expression[] parms = new Expression[] { right,
Expression.Constant(StringComparison.OrdinalIgnoreCase) };
EDIT: Here's a complete working sample:
using System;
using System.Linq.Expressions;
class Test
{
static void Main()
{
var method = typeof (string).GetMethod("IndexOf",
new[] { typeof (string), typeof(StringComparison) });
var left = Expression.Parameter(typeof(string), "left");
var right = Expression.Parameter(typeof(string), "right");
Expression[] parms = new Expression[] { right,
Expression.Constant(StringComparison.OrdinalIgnoreCase) };
var call = Expression.Call(left, method, parms);
var lambda = Expression.Lambda<Func<string, string, int>>
(call, left, right);
var compiled = lambda.Compile();
Console.WriteLine(compiled.Invoke("hello THERE", "lo t"));
}
}
The simplest way to do it would be to get it via a lambda like this:
//the compiler will convert the lambda into an expression
Expression<Func<string, string, int>> expression = (s1, s2) => s1.IndexOf(s2, StringComparison.OrdinalIgnoreCase);
//compile the expression so we can call it
var func = expression.Compile();
//outputs 2
Console.WriteLine(func("Case Sensitive", "se sensitive"));
This is much more readable and maintainable than manually building an expression tree.
I'm constantly surprised by the amount of people who dive straight into manually building expression trees. There's no need to when you can get the compiler to do the work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With