如何转换此Func& lt; SampleExpression,IEnumerator< string& gt;,bool& gt;& gt;到Func& lt; SampleExpression,bool& gt; [英] How do i convert this Func<SampleExpression,IEnumerator<string>,bool>> to Func<SampleExpression,bool>>

查看:53
本文介绍了如何转换此Func& lt; SampleExpression,IEnumerator< string& gt;,bool& gt;& gt;到Func& lt; SampleExpression,bool& gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的课程

class SampleExpression
{
    public string str;

    public static bool SampleEnum(SampleExpression s, IEnumerator<string> ien = null)
    {
        while (ien.MoveNext())
        {
            if (s.str == ien.Current)
            {
                ien.Reset();
                return true;
            }
        }
        return false;
    }
}

这是我在运行时生成表达式树的方式:

This is how i am generating my expression tree at runtime:

    static void Main(string[] args)
    {
        ParameterExpression param1 = Expression.Parameter(typeof(SampleExpression), "token");
        ParameterExpression param2 = Expression.Parameter(typeof(IEnumerator<string>), "args");

        var lstConstant = "1,2,3,4,".Split(new string[] { "," },
                           StringSplitOptions.RemoveEmptyEntries).ToList();

        var enummethod = typeof(SampleExpression).GetMethod("SampleEnum");
        MethodCallExpression methodCall = Expression.Call
                                        (
                                            enummethod,
                                            param1
                                            , param2
                                        );

        var e = Expression.Lambda<Func<SampleExpression, IEnumerator<string>, bool>>(methodCall, param1, param2);
        var l = e.Compile();

        List<SampleExpression> lst = new List<SampleExpression>();
        lst.Add(new SampleExpression { str = "1" }); // matches with lstConstant
        lst.Add(new SampleExpression { str = "2" }); // matches with lstConstant
        lst.Add(new SampleExpression { str = "5" });
        var items = lst.Where(x => l(x, lstConstant.GetEnumerator())).ToList();
    }

现在我可能会以复杂的方式完成此操作(因为我是Expression Tree的新手)-我的要求是:

Now i might i have done this in a convoluted way(cause i am novice in Expression trees) - my requirement is this:

我有一个用逗号分隔的字符串,例如"1,2,3,4," .我想将每个 SampleExpression 与类 SampleExpression 的字符串参数 str 进行匹配和匹配.到目前为止,我已经做到了.

I have a comma separated string like this "1,2,3,4,". I want to split and match each SampleExpression with the string parameter str of the class SampleExpression. Which i have done so far.

但是我希望表达式为 Func< SampleExpression,bool> .如您所见,当前它的 Func< SampleExpression,IEnumerator< string>,bool> .

However i want the Expression as Func<SampleExpression,bool>. As you can see currently its Func<SampleExpression, IEnumerator<string>, bool>.

我该如何解决.

推荐答案

表达式编译对我来说也很奇怪,但实际上要回答您的问题...

The expression compilation seems weird to me too, but to actually answer your question...

您可以像这样包装已编译的Func:

You can wrap the compiled Func like so:

Func<SampleExpression, bool> lBind = (SampleExpression token) => l(token, lstConstant.GetEnumerator());

这会将枚举器绑定为第二个参数,同时将第一个参数保留为打开状态.

This binds the enumerator as the second parameter, while leaving the first open for your input.

这篇关于如何转换此Func&amp; lt; SampleExpression,IEnumerator&lt; string&amp; gt;,bool&amp; gt;&amp; gt;到Func&amp; lt; SampleExpression,bool&amp; gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆