Flee
Flee copied to clipboard
Variable name with Non Alpha-numeric
I have a scenario like. var context = new ExpressionContext(); context.Variables["FieldSet1.Q1"] = 1; context.Variables["FieldSet1.Q2"] = 2; var expression = context.CompileDynamic("FieldSet1.Field1 + FieldSet1.Field2");
In this case. Is there an option to avoid parsing evaluation over the variables names because the expression is invalid and the message returned is:
IdentifierElement: Could not find a field/property/variable with the name 'FieldSet1'
Even the validation returned just a part of the variable: "FieldSet1"
Makes sense?
You can use wiki for this.
@mparlak I can't see this info over the wiki page. If you create a variable with a . or - and so on the expression is invalid.
@ffernandezcast you can use this.
Type type = typeof(FieldSet1);
ExpressionContext context = new ExpressionContext(); context.Options.ParseCulture = CultureInfo.InvariantCulture; context.ParserOptions.DecimalSeparator = '.'; context.Imports.AddType(type); context.Variables[type.Name] = fieldSet1;
IDynamicExpression compiledExpression = context.CompileDynamic("FieldSet1.Field1 + ieldSet1.Field2"); var result = compiledExpression.Evaluate();