ExpressionEval Function Call

1. Description

It’s possible to use function call in expressions, one or more. A function code have to be attached to each function call persent in the expression. When the expression is evaluated, for each function call found, the attached function code is executed. Function can have any, one or more parameters. Using function call is very easy to configure.

2. Function call signature

A function call has a name and parenthesis with any, one or more parameters until three. Parameters are separated by a comma. Here a some examples of function call in expressions:


Parameters of a function can be: a boolean, an integer, a string or a double.

3. Function Attachment

The attachment of a function code has to be done before evaluate expression. The attachment process is very basic to do, just call one method. The attachment has to be done only one time, it is available for all expressions evaluations executed after that.

For example the code below show a function code to attach, it’s nothing more than a standard C# function:

// the function to attach, one int parameter, return a bool
private bool FctDoSomething(int a)
   // do something...
   return true;

You can use it in expressions. Of course, the function call signature: parameters count and type and also the return type, should match exactly the function call signature. for example:

ExpressionEval evaluator = new ExpressionEval();


Next, you attach the function code to the function call, like this:

// attach the real function FctDoSomething() to the functionCall: Fct()
evaluator.AttachFunction("Fct", FctDoSomething);

As the example shows it, it’s possible to have different name between the function call used in expression and the function code to execute. Here, the function call is named: Fct and the function to execute is named: FctDoSomething().

Then you can evaluate the expression, the functionCall will execute the real function: FctDoSomething() and return here true:

ExecResult execResult = evaluator.Exec();

// should display: true
Console.WriteLine("Execution Result: " + execResult.ResultBool);

This way of attachment has to be used for function having, any, one or two parameters.

4. Attach function with three parameters

If the function has more than two parameters, you have to use another way to attach the function code to the function call. It’s because of how .NET implement callback function. You should use a mapper object to do that. For example, if you have to attach a function having three int parameters and returning an int, then you have to use this mapper:

// create a 3 params function mapper
Func3ParamsRetIntMapper<int, int, int> func3ParamsRetIntMapper = new Func3ParamsRetIntMapper<int, int, int>();

// set/add the function to the mapper

// attach the mapper to the function call
evaluator.AttachFunction("Fct", func3ParamsRetIntMapper);

There is a mapper for each type of return value : bool, int string and double.

// return type is a bool
class Func3ParamsRetBoolMapper<TypeParam1, TypeParam2, TypeParam3>

// return type is an int
class Func3ParamsRetIntMapper<TypeParam1, TypeParam2, TypeParam3>

// return type is a string
class Func3ParamsRetStringMapper<TypeParam1, TypeParam2, TypeParam3>

// return type is a double
class Func3ParamsRetDoubleMapper<TypeParam1, TypeParam2, TypeParam3> 

The function code should be something like that, nothing more than a standard C# function having three parameters and returning an int value:

// define your function
// has three int parameters and returning an int value
private int FctRetInt_Int_Int_Int(int a, int b, int c)
  // define the behavior of the function...
  return a + b + c;

An expression using this function:

Fct(1, 3, 5)

The evaluation of the expression is not different than evaluate a function with any, one, or two parameters:

 ExecResult execResult = evaluator.Exec();
 Console.WriteLine("Execution Result is: " + execResult.ResultInt);

The output console should displays the result, in the current example, it’s the sum of the all parameters values:

Execution Result is: 9 

5. Error management

Using function call in bad way should generate errors. Different error case are managed by the evaluator component. A list of error code is available. See the dedicated page describing these errors.

6. Clear all function attachment

Funtion atachments are available for all expressions you evaluate starting from the attachment. It’s possible to remove all function attachment with this instruction: