ExpressionPropertyEditor. Custom Function

The developer of the business application can implement his own functions to extend the set of the built-in functions available in the expression editor. The custom function is a class implementing the ICustomFunctionOperatorBrowsable interface. Once a custom function is declared, you need to register it in your application using the CriteriaOperator.RegisterCustomFunction static method.

The following example shows, how to implement and the AddNumbersFunction function, which returns the sum of two numbers passed as parameters.

Implementation:

  • c#
  • VB

public class AddNumbersFunction : ICustomFunctionOperatorBrowsable
{
  public Type ResultType(params Type[] operands)
  {
    return typeof(int);
  }
  public object Evaluate(params object[] operands)
  {
    return ((int)operands[0]) + ((int)operands[1]);
  }
  public string Name
  {
    get
    {
      return "AddNumbers";
    }
  }
  public bool IsValidOperandCount(int count)
  {
    return count == 2;
  }
  public bool IsValidOperandType(int operandIndex, int operandCount, Type type)
  {
    if (operandCount != 2)
    {
      return false;
    }
    return ((operandIndex == 0) || ((operandIndex == 1) && (type == typeof(int))));
  }
  public int MinOperandCount
  {
    get
    {
      return 2;
    }
  }
  public int MaxOperandCount
  {
    get
    {
      return 2;
    }
  }
  public string Description
  {
    get
    {
      return CaptionHelper.GetLocalizedText("Texts", "AddNumberFunction");
    }
  }
  public FunctionCategory Category
  {
    get
    {
      return FunctionCategory.All;
    }
  }
}

Public Class AddNumbersFunction
  Implements ICustomFunctionOperatorBrowsable
  Public Function ResultType(ParamArray operands As Type()) As Type
    Return GetType(Integer)
  End Function
  Public Function Evaluate(ParamArray operands As Object()) As Object
    Return CType(operands(0), Integer) + CType(operands(1), Integer)
  End Function
  Public ReadOnly Property Name As String
    Get
      Return "AddNumbers"
    End Get
  End Property
  Public Function IsValidOperandCount(ByVal count As Integer) As Boolean
    Return count = 2
  End Function
  Public Function IsValidOperandType(ByVal operandIndex As Integer, ByVal operandCount As Integer, ByVal type As Type) As Boolean
    If operandCount <> 2 Then
      Return False
    End If
    Return ((operandIndex = 0) OrElse ((operandIndex = 1) AndAlso (type = GetType(Integer))))
  End Function
  Public ReadOnly Property MinOperandCount As Integer
    Get
      Return 2
    End Get
  End Property
  Public ReadOnly Property MaxOperandCount As Integer
    Get
      Return 2
    End Get
  End Property
  Public ReadOnly Property Description As String
    Get
      Return CaptionHelper.GetLocalizedText("Texts", "AddNumberFunction")
    End Get
  End Property
  Public ReadOnly Property Category As FunctionCategory
    Get
      Return FunctionCategory.All
    End Get
  End Property
End Class

Registration in Module.cs:

  • c#
  • VB

public override void Setup(XafApplication application)
{
  //...
  var customFunction = new AddNumbersFunction();
  if (CriteriaOperator.GetCustomFunction(customFunction.Name) == null)
    CriteriaOperator.RegisterCustomFunction(customFunction);
  //...
}

Public Overrides Sub Setup(ByVal application As XafApplication)
  '...
  Dim customFunction = New AddNumbersFunction()
  If CriteriaOperator.GetCustomFunction(customFunction.Name) Is Nothing Then
    CriteriaOperator.RegisterCustomFunction(customFunction)
  End If
  '...
End Sub

You can see the code used in this topic in the AddNumbersFunction.cs and Module.cs files of the Feature Center demo installed with Xafari.