Skip to content

Options

Coding Seb edited this page Jun 13, 2022 · 22 revisions

ExpressionEvaluator has a set of options that can modify the way the evaluator works.
Here is the list :

OptionCaseSensitiveEvaluationActive

Type : Boolean
Default value : true

When true : The code evaluation is case sensitive :

Example :

// Works not
abs(-1) + SIN(3 * pi) - cOS(2 * pI)
// Must be written as this
Abs(-1) + Sin(3 * Pi) - Cos(2 * Pi)

When false : The code evaluation is case insensitive :

Example :

// Works
abs(-1) + SIN(3 * pi) - cOS(2 * pI)

OptionVariablesPersistenceCustomComparer

Type : Boolean
Default value : false

When true : evaluator.Variables dictionary is kept as given so variables are persist outside of the evaluator and the comparer for keys can be defined by the user

Example :

var persist = new Dictionary<string,object>()
{
    {"x", 1}
};

evaluator.Variables = persist;

evaluator.Evaluate("x = 2");

// This will print : 2
Console.WriteLine(persist["x"]);

When false : evaluator.Variables dictionary references are copied internally to follow OptionCaseSensitiveEvaluationActive with an internal protected comparer for keys

Example :

var persist = new Dictionary<string,object>()
{
    {"x", 1}
};

evaluator.Variables = persist;

evaluator.Evaluate("x = 2");

// This will print : 1
Console.WriteLine(persist["x"]);

OptionForceIntegerNumbersEvaluationsAsDoubleByDefault

Type : Boolean
Default value : false

When true All numbers without decimal or suffixes are evaluated as double :

Example :

// Equals 0.2
10 / 50

When false : Integers values without decimal or suffixes are evaluated as int as in C# :

Example :

// Equals 0
10 / 50

Available from version 1.3.5.0

CultureInfoForNumberParsing

Type : CultureInfo
Default value : CultureInfo.CultureInvariant.Clone()

The culture used to evaluate numbers.

Remark : Synchronized with OptionNumberParsingDecimalSeparator and OptionNumberParsingThousandSeparator.
So always set a full CultureInfo object and do not change CultureInfoForNumberParsing.NumberFormat.NumberDecimalSeparator and CultureInfoForNumberParsing.NumberFormat.NumberGroupSeparator properties directly.

Warning if using comma in separators change also OptionFunctionArgumentsSeparator and OptionInitializersSeparator otherwise it will create conflicts Warning it's your responsability to avoid conflicts with operators or other syntax stuff.

Available from version 1.3.4.0

OptionNumberParsingDecimalSeparator

Type : string
Default value : "."

Allow to change the decimal separator of numbers when parsing expressions.

Remark : Modify CultureInfoForNumberParsing

Warning you use comma change also OptionFunctionArgumentsSeparator and OptionInitializersSeparator otherwise it will create conflicts
Warning it's your responsability to avoid conflicts with operators or other syntax stuff.

Available from version 1.3.4.0

OptionNumberParsingThousandSeparator

Type : string
Default value : string.Empty

Allow to change the thousand separator of numbers when parsing expressions.

Warning if you use comma change also OptionFunctionArgumentsSeparator and OptionInitializersSeparator otherwise it will create conflicts
Warning it's your responsability to avoid conflicts with operators or other syntax stuff.

Available from version 1.3.4.0

OptionFunctionArgumentsSeparator

Type : string
Default value : ","

Allow to change the separator of functions arguments.

Warning must to be changed if OptionNumberParsingDecimalSeparator or OptionNumberParsingThousandSeparator are set to "," otherwise it will create conflicts
Warning it's your responsability to avoid conflicts with operators or other syntax stuff.

Available from version 1.3.4.0

OptionInitializersSeparator

Type : string
Default value : ","

Allow to change the separator of Object and collections Initialization between { and } after the keyword new.

Warning must to be changed if OptionNumberParsingDecimalSeparator or OptionNumberParsingThousandSeparator are set to "," otherwise it will create conflicts
Warning it's your responsability to avoid conflicts with operators or other syntax stuff.

Available from version 1.3.4.0

OptionInlineNamespacesEvaluationActive

Type : Boolean
Default value : true

When true Allow the use of any inline namespace that is available in memory

When false The use of inline namespace is forbidden

Replaced by OptionInlineNamespacesEvaluationRule in version 1.4.38.0

OptionInlineNamespacesEvaluationRule

Type : InlineNamespacesEvaluationRule (enum)
Default value : AllowAll

When AllowAll
Allow the use of any inline namespace that is available in memory

When AllowOnlyInlineNamespacesList
Allow only the use of inline namespace defined in the list InlineNamespacesList (List<string>) Example :

evaluator.OptionInlineNamespacesEvaluationRule = InlineNamespacesEvaluationRule.AllowOnlyInlineNamespacesList;
evaluator.InlineNamespacesList = new List<string>() { "System.Text.RegularExpressions" };  
  
// Works return a Regex
evaluator.Evaluate("new System.Text.RegularExpressions.Regex(\"[A-Z]\\d+\")");
// Will throw an exception because the namespace System.Diagnostics is not define in InlineNamespacesList 
evaluator.Evaluate("System.Diagnostics.Process.Start(\"mspaint\")");

When BlockOnlyInlineNamespacesList
Allow the use of any inline namespace that is available in memory that is not defined in InlineNamespacesList (List<string>) Example :

evaluator.OptionInlineNamespacesEvaluationRule = InlineNamespacesEvaluationRule.BlockOnlyInlineNamespacesList;
evaluator.InlineNamespacesList = new List<string>() { "System.Text.RegularExpressions" };  
  
// Will throw an exception because the namespace System.Text.RegularExpressions is define in InlineNamespacesList and so is blocked
evaluator.Evaluate("new System.Text.RegularExpressions.Regex(\"[A-Z]\\d+\")");
// Works will launch the application MsPaint
evaluator.Evaluate("System.Diagnostics.Process.Start(\"mspaint\")");

When BlockAll The use of inline namespace is forbidden

Available from version 1.4.38.0 (For older version see OptionInlineNamespacesEvaluationActive )

OptionFluidPrefixingActive

Type : Boolean
Default value : true

When true : Allow to prefix void methods with Fluid or Fluent :

Example :

// Works return 3
List("hello", "bye").FluidAdd("test").Count

When false : Disable the functionality :

Example :

// Generate an exception
List("hello", "bye").FluidAdd("test").Count

When true : Allow to use inline namespaces before type (class). you can use it :

  • with new for object creation
  • for casting type
  • for static call on a class
  • for any reference to a type or class for which the namespace is not defined in evaluator.Namespaces.

When false : Disable this functionality. So only the namespaces declared in evaluator.Namespaces are available.

See Inline namespaces

OptionNewFunctionEvaluationActive

Type : Boolean
Default value : true

When true : Allow to create objects with the new() function :

Example :

// Works
new(typeof(MyObject))

When false : new as a standard function is forbidden

Example :

// Generate an exception
new(typeof(MyObject))

OptionNewKeywordEvaluationActive

Type : Boolean
Default value : true

When true : Allow to create objects with the new keyword :

Example :

// Works
new MyObject()

When false : new as a keyword is forbidden

Example :

// Generate an exception
new MyObject()

OptionStaticMethodsCallActive

Type : Boolean
Default value : true

When true : Allow to call static methods on classes.

Example :

// Works
string.Format("Hello {0}", name)

When false : Calling static methods is forbidden

Example :

// Generate an exception
string.Format("Hello {0}", name)

OptionStaticPropertiesGetActive

Type : Boolean
Default value : true

When true : Allow to get the value of a static property on classes.

Example :

// Works
string.Empty

When false : Getting the value of a static property is forbidden.

Example :

// Generate an exception
string.Empty

OptionInstanceMethodsCallActive

Type : Boolean
Default value : true

When true : Allow to call methods on objects.

Example :

// Works
myvar.ToString()

When false : Calling instance methods is forbidden.

Example :

// Generate an exception
myvar.ToString()

OptionInstancePropertiesGetActive

Type : Boolean
Default value : true

When true : Allow to get the value a property on objects.

Example :

// Works
myStringVar.Length

When false : Getting the value of an instance property is forbidden.

Example :

// Generate an exception
myStringVar.Length

OptionIndexingActive

Type : Boolean
Default value : true

When true : Allow to get the value at a specific index of indexed object (Array, List, Dictionnary ...)

Example :

// Works
myArray[2]
myDictionnary["key"]

When false : Indexing is forbidden.

Example :

// Generate exceptions
myArray[2]
myDictionnary["key"]

OptionStringEvaluationActive

Type : Boolean
Default value : true

When true : Allow double quotes to evaluate string values ("", $"", @"")

Example :

// Works
"Hello"
$"Hello {name}"
@"C:\Windows"

When false : string values are not evaluated

Example :

// Generate exceptions
"Hello"
$"Hello {name}"
@"C:\Windows"

OptionCharEvaluationActive

Type : Boolean
Default value : true

When true : Allow single quotes to evaluate string values ('')

Example :

// Works
'a'
'\n'
','

When false : char values are not evaluated

Example :

// Generate exceptions
'a'
'\n'
','

OptionEvaluateFunctionActive

Type : Boolean
Default value : true

When true : Allow to use the function Evaluate in an expression

Example :

// Works
Evaluate("1+2")

When false : The Evaluate function is blocked. If set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)

Example :

// Generate exceptions
Evaluate("1+2")

OptionVariableAssignationActive

Type : Boolean
Default value : true

When true : Allow to assign a value to a variable in the Variable disctionary with (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, ++ or --)

Example :

// Works
text = "Hello"
i++

When false : Block the assignation of variables

Example :

// Generate exceptions
text = "Hello"
i++

OptionPropertyOrFieldSetActive

Type : Boolean
Default value : true

When true : Allow to set/modify a property or a field value with (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, ++ or --)

Example :

// Works
myObject.Text = "Hello"

When false : Block the assignation of fields and properties

Example :

// Generate exceptions
myObject.Text = "Hello"

OptionIndexingAssignationActive

Type : Boolean
Default value : true

When true : Allow to assign a indexed element like Collections, List, Arrays and Dictionaries with (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, ++ or --)

Example :

// Works
myArray[2] = "Hello"
myDictionnary["key"]++;

When false : Block the assignation of indexed element

Example :

// Generate exceptions
myArray[2] = "Hello"
myDictionnary["key"]++;

OptionScriptEvaluateFunctionActive

Type : Boolean
Default value : true

When true : Allow to use the function ScriptEvaluate in an expression

Example :

// Works
ScriptEvaluate("value = 1+2;\r\nif(value > 2)\r\nreturn \"OK\";\r\nelse\r\nreturn \"NOK\";")

When false : The ScriptEvaluate function is blocked. If set to false for security (also ensure that ExpressionEvaluator type is in [TypesToBlock]((./C%23-Types-Management#Block-access-of-a-some-type) list)

Example :

// Generate exceptions
ScriptEvaluate("value = 1+2;\r\nif(value > 2)\r\nreturn \"OK\";\r\nelse\r\nreturn \"NOK\";")

OptionOnNoReturnKeywordFoundInScriptAction

Type : OptionOnNoReturnKeywordFoundInScriptAction
Default value : ReturnAutomaticallyLastEvaluatedExpression

Set How to react when the keyword return is not found in a script. when using ScriptEvaluate method

When ReturnAutomaticallyLastEvaluatedExpression : Return the last evaluated expression.

Example :

// For script :
x = 1;
y = 2;
x+y;
// return 3

When ReturnNull : Return null

Example :

// For script :
x = 1;
y = 2;
x+y;
// return null

When ThrowSyntaxException : Generate an exception

Example :

// This script generate an exception :
x = 1;
y = 2;
x+y;

OptionScriptNeedSemicolonAtTheEndOfLastExpression

Type : Boolean
Default value : true

When true : The last expression of a script must end with a semicolon [;]

Example :

x = 1;
y = 2;
// Generate an ExpressionEvaluatorSyntaxErrorException -> "A [;] character is missing."
x+y

When false : The last expression of a script don't need to end with a semicolon [;]

Example :

x = 1;
y = 2;
// return 3"
x+y

Available from version 1.4.3.0

OptionAllowNonPublicMembersAccess

Type : Boolean
Default value : false

See Live example here

When true : Allow to get or set values on private and protected members and call private and protected methods. It works on all given Variables and on the Context object. It works also on all static members and methods.

When false : Only allow access on public members and methods

Warning : This break the encapsulation principle so use it only if you know what you do.

Available from version 1.4.7.0

Table Of Content

Clone this wiki locally