如何转换此Func< SampleExpression,IEnumerator< string>,bool>到Func< Samp

编程入门 行业动态 更新时间:2024-10-10 11:25:19
本文介绍了如何转换此Func< SampleExpression,IEnumerator< string>,bool>到Func< SampleExpression,bool>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的课程

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&lt; SampleExpression,IEnumerator&lt; string&gt;,bool&g

本文发布于:2023-11-12 13:01:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   SampleExpression   Func   amp   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!