C# Класс CalcEngine.CalcEngine

CalcEngine parses strings and returns Expression objects that can be evaluated.

This class has three extensibility points:

Use the DataContext property to add an object's properties to the engine scope.

Use the RegisterFunction method to define custom functions.

Override the GetExternalObject method to add arbitrary variables to the engine scope.

Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
CalcEngine ( ) : System
Evaluate ( string expression ) : object

Evaluates a string.

If you are going to evaluate the same expression several times, it is more efficient to parse it only once using the Parse method and then using the Expression.Evaluate method to evaluate the parsed expression.

GetExternalObject ( string identifier ) : object

Gets an external object based on an identifier.

This method is useful when the engine needs to create objects dynamically. For example, a spreadsheet calc engine would use this method to dynamically create cell range objects based on identifiers that cannot be enumerated at design time (such as "AB12", "A1:AB12", etc.)

Parse ( string expression ) : Expression

Parses a string into an Expression.

RegisterFunction ( string functionName, int parmCount, CalcEngineFunction fn ) : void

Registers a function that can be evaluated by this CalcEngine.

RegisterFunction ( string functionName, int parmMin, int parmMax, CalcEngineFunction fn ) : void

Registers a function that can be evaluated by this CalcEngine.

Приватные методы

Метод Описание
AddToken ( object symbol, TKID id, TKTYPE type ) : void
GetFunctionTable ( ) : FunctionDefinition>.Dictionary
GetMember ( ) : CalcEngine.Token
GetParameters ( ) : List
GetSymbolTable ( ) : Token>.Dictionary
GetToken ( ) : void
ParseAddSub ( ) : Expression
ParseAtom ( ) : Expression
ParseCompare ( ) : Expression
ParseDouble ( string str, CultureInfo ci ) : double
ParseExpression ( ) : Expression
ParseMulDiv ( ) : Expression
ParsePower ( ) : Expression
ParseUnary ( ) : Expression
Throw ( ) : void
Throw ( string msg ) : void

Описание методов

CalcEngine() публичный Метод

public CalcEngine ( ) : System
Результат System

Evaluate() публичный Метод

Evaluates a string.
If you are going to evaluate the same expression several times, it is more efficient to parse it only once using the Parse method and then using the Expression.Evaluate method to evaluate the parsed expression.
public Evaluate ( string expression ) : object
expression string Expression to evaluate.
Результат object

GetExternalObject() публичный Метод

Gets an external object based on an identifier.
This method is useful when the engine needs to create objects dynamically. For example, a spreadsheet calc engine would use this method to dynamically create cell range objects based on identifiers that cannot be enumerated at design time (such as "AB12", "A1:AB12", etc.)
public GetExternalObject ( string identifier ) : object
identifier string
Результат object

Parse() публичный Метод

Parses a string into an Expression.
public Parse ( string expression ) : Expression
expression string String to parse.
Результат Expression

RegisterFunction() публичный Метод

Registers a function that can be evaluated by this CalcEngine.
public RegisterFunction ( string functionName, int parmCount, CalcEngineFunction fn ) : void
functionName string Function name.
parmCount int Parameter count.
fn CalcEngineFunction Delegate that evaluates the function.
Результат void

RegisterFunction() публичный Метод

Registers a function that can be evaluated by this CalcEngine.
public RegisterFunction ( string functionName, int parmMin, int parmMax, CalcEngineFunction fn ) : void
functionName string Function name.
parmMin int Minimum parameter count.
parmMax int Maximum parameter count.
fn CalcEngineFunction Delegate that evaluates the function.
Результат void