问题
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
回答1:
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"));
}
}
回答2:
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.
回答3:
I didn't check the rest of it, but if only the enum poses problems:
Expression.Constant(StringComparison.OrdinalIgnoreCase)
or
Expression.Constant(Enum.Parse(typeof(StringComparison), "OrdinalIgnoreCase"), typeof(Enum));
And you have more options. Or check my answer here.
Edit: forgot paranthesis.
来源:https://stackoverflow.com/questions/7089225/expression-tree-for-string-indexof-method