开发者

Expression tree for String.IndexOf method

开发者 https://www.devze.com 2023-03-28 10:30 出处:网络
How should I construct Expression tree for string.IndexOf(\"substring\", StringComparison.OrdinalIgnoreCase)?

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[] p开发者_高级运维arms = 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号