Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Coding Seb edited this pageJun 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 notabs(-1)+SIN(3*pi)-cOS(2*pI)// Must be written as thisAbs(-1)+Sin(3*Pi)-Cos(2*Pi)

When false :The code evaluation is case insensitive :

Example :

// Worksabs(-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 :

varpersist=newDictionary<string,object>(){{"x",1}};evaluator.Variables=persist;evaluator.Evaluate("x = 2");// This will print : 2Console.WriteLine(persist["x"]);

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

Example :

varpersist=newDictionary<string,object>(){{"x",1}};evaluator.Variables=persist;evaluator.Evaluate("x = 2");// This will print : 1Console.WriteLine(persist["x"]);

OptionForceIntegerNumbersEvaluationsAsDoubleByDefault

Type :Boolean
Default value :false

When trueAll numbers without decimal or suffixes are evaluated as double :

Example :

// Equals 0.210/50

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

Example :

// Equals 010/50

Available from version 1.3.5.0

CultureInfoForNumberParsing

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

The culture used to evaluate numbers.

Remark : Synchronized withOptionNumberParsingDecimalSeparator andOptionNumberParsingThousandSeparator.
So always set a full CultureInfo object and do not changeCultureInfoForNumberParsing.NumberFormat.NumberDecimalSeparator andCultureInfoForNumberParsing.NumberFormat.NumberGroupSeparator properties directly.

Warning if using comma in separators change also OptionFunctionArgumentsSeparator and OptionInitializersSeparator otherwise it will create conflictsWarning 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 : ModifyCultureInfoForNumberParsing

Warning you use comma change alsoOptionFunctionArgumentsSeparator andOptionInitializersSeparator 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 alsoOptionFunctionArgumentsSeparator andOptionInitializersSeparator 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 ifOptionNumberParsingDecimalSeparator orOptionNumberParsingThousandSeparator 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 ifOptionNumberParsingDecimalSeparator orOptionNumberParsingThousandSeparator 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

WhentrueAllow the use of anyinline namespace that is available in memory

WhenfalseThe use ofinline namespace is forbidden

Replaced byOptionInlineNamespacesEvaluationRule in version 1.4.38.0

OptionInlineNamespacesEvaluationRule

Type :InlineNamespacesEvaluationRule (enum)
Default value :AllowAll

WhenAllowAll
Allow the use of anyinline namespace that is available in memory

WhenAllowOnlyInlineNamespacesList
Allow only the use ofinline namespace defined in the listInlineNamespacesList (List<string>)Example :

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

WhenBlockOnlyInlineNamespacesList
Allow the use of anyinline namespace that is available in memory that is not defined inInlineNamespacesList (List<string>)Example :

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

WhenBlockAllThe use ofinline namespace is forbidden

Available from version 1.4.38.0 (For older version seeOptionInlineNamespacesEvaluationActive )

OptionFluidPrefixingActive

Type :Boolean
Default value :true

When true :Allow to prefix void methods withFluid orFluent :

Example :

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

When false :Disable the functionality :

Example :

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

When true :Allow to useinline 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 inevaluator.Namespaces.

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

SeeInline namespaces

OptionNewFunctionEvaluationActive

Type :Boolean
Default value :true

When true :Allow to create objects with thenew() function :

Example :

// Worksnew(typeof(MyObject))

When false :new as a standard function is forbidden

Example :

// Generate an exceptionnew(typeof(MyObject))

OptionNewKeywordEvaluationActive

Type :Boolean
Default value :true

When true :Allow to create objects with thenew keyword :

Example :

// WorksnewMyObject()

When false :new as a keyword is forbidden

Example :

// Generate an exceptionnewMyObject()

OptionStaticMethodsCallActive

Type :Boolean
Default value :true

When true :Allow to call static methods on classes.

Example :

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

When false :Calling static methods is forbidden

Example :

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

OptionStaticPropertiesGetActive

Type :Boolean
Default value :true

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

Example :

// Worksstring.Empty

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

Example :

// Generate an exceptionstring.Empty

OptionInstanceMethodsCallActive

Type :Boolean
Default value :true

When true :Allow to call methods on objects.

Example :

// Worksmyvar.ToString()

When false :Calling instance methods is forbidden.

Example :

// Generate an exceptionmyvar.ToString()

OptionInstancePropertiesGetActive

Type :Boolean
Default value :true

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

Example :

// WorksmyStringVar.Length

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

Example :

// Generate an exceptionmyStringVar.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 :

// WorksmyArray[2]myDictionnary["key"]

When false :Indexing is forbidden.

Example :

// Generate exceptionsmyArray[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 :

// WorksEvaluate("1+2")

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

Example :

// Generate exceptionsEvaluate("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 :

// Workstext="Hello"i++

When false :Block the assignation of variables

Example :

// Generate exceptionstext="Hello"i++

OptionPropertyOrFieldSetActive

Type :Boolean
Default value :true

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

Example :

// WorksmyObject.Text="Hello"

When false :Block the assignation of fields and properties

Example :

// Generate exceptionsmyObject.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 :

// WorksmyArray[2]="Hello"myDictionnary["key"]++;

When false :Block the assignation of indexed element

Example :

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

OptionScriptEvaluateFunctionActive

Type :Boolean
Default value :true

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

Example :

// WorksScriptEvaluate("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 exceptionsScriptEvaluate("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 usingScriptEvaluate method

When ReturnAutomaticallyLastEvaluatedExpression :Return the last evaluated expression.

Example :

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

When ReturnNull :Returnnull

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 givenVariables and on theContext 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

[8]ページ先頭

©2009-2025 Movatter.jp