Overview

Version 0.9.7.0 of ILCalc library introduce the new feature - generics support.
It allows you to evaluate the expressions, containing elements not only of the Double type.
But note, that expressions in ILCalc can contains items only of one type, mixing different types is not possible!

Usage

All that we needed - just specify the expression type when creating context instance:

var doubleCalc = new CalcContext<double>("x");
var int32Calc  = new CalcContext<int>("i");

double resDouble = doubleCalc.Evaluate("2+x", 1.23);  // = 3.23
int resInt32 = int32Calc.Evaluate("i*2", 8);         // = 16
All the contents of CalcContext's class now are generalized too.
For example Constants property returns ConstantCollection<T>:

doubleCalc.Constants.Add("pi", 3.14); // only double values here
int32Calc.Constants.Add("min", int.MinValue); // only integers here
FunctionCollection<T> can only contains functions with parameters and return type of T type:

// imports 23 math functions (sin, cos, ...)
doubleCalc.Functions.Import(typeof(System.Math));

// imports 4 functions that is suitable for Int32 expression: Abs, Max, Min, Sign
int32Calc.Functions.Import(typeof(System.Math));
Evaluator<T> and Interpret<T> returns the evaluated values of T type:

Evaluator<int> eval = int32Calc.CreateEvaluator("i+0xFFFF");
int result32 = eval.Evaluate(0xABCD);

Support

ILCalc currently fully supported only few primitive BCL types as T parameter: Int32, Int64, Single, Double, Decimal.

What means "fully supported"?
It means that this types can be presented as literals in the expression string, combined with operators, evaluated in the interpreter and compiled into MSIL.
Other types may be used too, but operators, literals and some compilation features will be not available. Only constants, arguments and functions will works well, for example:

var forFun = new CalcContext<Stopwatch>();
forFun.Functions.Add("start", Stopwatch.StartNew);

Stopwatch sw = forFun.Evaluate("start()");
Console.WriteLine(sw.Elapsed);
What about other types support?
Some of other primitive types will be supported in the future releases natively.
For other types will be introduced support for providing custom implementation and discovering operators automatically.

Last edited Sep 9, 2009 at 4:04 PM by Pelmen64, version 2

Comments

No comments yet.