Search code examples
c#vb.netparsinggenerics.net-2.0

Expression parsing using FLEE and custom expression context


I am using Flee to build a formula builder. It works great but the only problem I'm facing is that Flee doesn't understand Generic Methods I guess.

I have a function called IIf declared in the expression context I'm using.

Public Function IIf(Of T)(ByVal cond As Boolean, ByVal left As T, ByVal right As T) As T
    Return If(cond, left, right)
End Function

Now I'm lets say I'm evaluating

IIF(A==B,1,5)

When I'm trying to compile the expression, Flee is throwing an ExpressionException stating that -

The function IIf(boolean, Int32, Int32) is not declared.

How can I work around this. I mean cannot , in sense, write all possible overloads of the function of all .net primitive types. What approach should I take.


Solution

  • In the discussions about FLEE, one poster managed to get Generics working. Check out http://flee.codeplex.com/discussions/14611. Effectively, the poster declared the containing class as a Generic.

    Alternatively, there may not be a way depending on how your class is structured as per this discussion: http://flee.codeplex.com/discussions/355342 which states that the poster had to declare public overloads for primitive datatypes.

    You might also be able to get away with declaring a generic method with objects and turning off Option Strict to allow implicit casting like so:

    Public Function IIf(ByVal cond As Boolean, ByVal left As Object, ByVal right As Object) As Object
        Return If(cond, left, right)
    End Function