从Func构建条件表达式... [英] Building Conditional Expressions from Func...
本文介绍了从Func构建条件表达式...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图进入表达式,IL生成的代表等。
我有这个原型用于组合'比较',但我认为它很慢,即使只需2-3个功能:
(我用VB - 抱歉;))
Trying to get into Expressions, IL-Generated Delegates etc.
I have this prototype for combining 'Comparisons', but I think it's quite slow, even with just 2-3 Funcs:
(I use VB -- sorry ;) )
Public Function BuildCompareExpression(Of T)(exp1 As Expression(Of Func(Of T, T, Integer)), exp2 As Expression(Of Func(Of T, T, Integer))) As Expression(Of Func(Of T, T, Integer))
Dim parameters() = {Expression.Parameter(GetType(T)), Expression.Parameter(GetType(T))}
Dim invoked = Expression.Invoke(exp1, parameters)
Dim invoked2 = Expression.Invoke(exp2, parameters)
Dim isTrue = Expression.Equal(Expression.Constant(0), invoked)
Dim conditional = Expression.Condition(isTrue, invoked2, invoked)
Dim lambda = Expression.Lambda(Of Func(Of T, T, Integer))(conditional, parameters)
While lambda.CanReduce
lambda = CType(lambda.Reduce,Expression(Of Global.System.Func(Of T, T, Integer)))
End While
Return lambda
End Function
OBS!当然,在添加最后一个 Func(Of T,T,Integer)
后,我会'lambda.Compile'
希望任何人都有更好/更快的选择。
提前致谢。
丹麦Jens。
OBS! Of course, upon adding the last Func(Of T, T, Integer)
, I do 'lambda.Compile'
Hope anyone has some better/faster alternative.
Thanks in advance.
Jens, Denmark.
推荐答案
这篇关于从Func构建条件表达式...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文