Skip to content

Commit 804fed9

Browse files
author
Sébastien Geiser
committed
ExpandoObjects init and use of existing variables in init
1 parent c676fdc commit 804fed9

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.3.6.0
3+
Version : 1.3.6.1
44
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66
Author : Coding Seb
@@ -1553,13 +1553,17 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
15531553

15541554
void Init(object element, List<string> initArgs)
15551555
{
1556-
if (typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IDictionary).IsAssignableFrom(type))
1556+
if (typeof(IEnumerable).IsAssignableFrom(type)
1557+
&& !typeof(IDictionary).IsAssignableFrom(type)
1558+
&& !typeof(ExpandoObject).IsAssignableFrom(type))
15571559
{
15581560
MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
15591561

15601562
initArgs.ForEach(subExpr => methodInfo.Invoke(element, new object[] { Evaluate(subExpr) }));
15611563
}
1562-
else if(typeof(IDictionary).IsAssignableFrom(type) && initArgs.All(subExpr => subExpr.TrimStart().StartsWith("{")))
1564+
else if(typeof(IDictionary).IsAssignableFrom(type)
1565+
&& initArgs.All(subExpr => subExpr.TrimStart().StartsWith("{"))
1566+
&& !typeof(ExpandoObject).IsAssignableFrom(type))
15631567
{
15641568
initArgs.ForEach(subExpr =>
15651569
{
@@ -1583,19 +1587,23 @@ void Init(object element, List<string> initArgs)
15831587
}
15841588
else
15851589
{
1586-
ExpressionEvaluator initEvaluator = new ExpressionEvaluator(new Dictionary<string, object>() { { "this", element } });
1590+
string variable = "V" + Guid.NewGuid().ToString().Replace("-", "");
1591+
1592+
Variables[variable] = element;
15871593

15881594
initArgs.ForEach(subExpr =>
15891595
{
15901596
if (subExpr.Contains("="))
15911597
{
15921598
string trimmedSubExpr = subExpr.TrimStart();
15931599

1594-
initEvaluator.Evaluate($"this{(trimmedSubExpr.StartsWith("[") ? string.Empty : ".")}{trimmedSubExpr}");
1600+
Evaluate($"{variable}{(trimmedSubExpr.StartsWith("[") ? string.Empty : ".")}{trimmedSubExpr}");
15951601
}
15961602
else
15971603
throw new ExpressionEvaluatorSyntaxErrorException($"A '=' char is missing in [{subExpr}]. It is in a object initializer. It must contains one.");
15981604
});
1605+
1606+
Variables.Remove(variable);
15991607
}
16001608
}
16011609

0 commit comments

Comments
 (0)