Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ;) )

VB
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! 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.
Posted
Updated 23-Apr-14 12:41pm
v2
Comments
Matt T Heffron 23-Apr-14 20:48pm    
This is interesting...
Can you give a little bit of why you're doing this so we can think in the same "problem space"?
Jens Madsen, Højby 24-Apr-14 10:22am    
Basically, I want to build comparisons on the fly, so to say.
Enumerating properties from an 'unknown' (at compile time) object, finding properties (or fields) that implement IComparable (Or CompareTo(Of WhatEver) and from these members build a complex comparison defined at runtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900