Skip to content

Commit 7de114d

Browse files
committed
#104 Ok with this keyword. this keyword is automatically set as the context object
1 parent 9801486 commit 7de114d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,7 @@ public void Evaluate_ParameterTypeMismatchAutoAdapt()
16431643

16441644
/// <summary>
16451645
/// To correct #104 Extension methods do not work on context
1646+
/// work now with this keyword like in C#
16461647
/// </summary>
16471648
[Test]
16481649
[Category("Bug")]
@@ -1657,10 +1658,28 @@ public void Evaluate_ExtensionMethodOnContext()
16571658
}
16581659
};
16591660

1660-
evaluator.Evaluate("Sum()")
1661+
evaluator.Evaluate("this.Sum()")
16611662
.ShouldBe(15);
16621663
}
16631664

1665+
/// <summary>
1666+
/// To correct #104 Indexing do not work on context
1667+
/// work now with this keyword like in C#
1668+
/// </summary>
1669+
[Test]
1670+
[Category("Bug")]
1671+
[Category("#104")]
1672+
public void Evaluate_IndexingOnContext()
1673+
{
1674+
var evaluator = new ExpressionEvaluator
1675+
{
1676+
Context = new ClassForIndexing()
1677+
};
1678+
1679+
evaluator.Evaluate("this[\"Test\"]")
1680+
.ShouldBe("TestTest");
1681+
}
1682+
16641683
/// <summary>
16651684
/// To correct #105 Exception were not thrown when concat with string
16661685
/// </summary>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ protected enum TryBlockEvaluatedState
336336
{ "null", null},
337337
{ "true", true },
338338
{ "false", false },
339+
{ "this", null }
339340
};
340341

341342
protected IDictionary<string, Func<double, double>> simpleDoubleMathFuncsDictionary = new Dictionary<string, Func<double, double>>(StringComparer.Ordinal)
@@ -872,10 +873,20 @@ protected virtual BindingFlags StaticBindingFlag
872873

873874
#region Custom and on the fly evaluation
874875

876+
private object context;
877+
875878
/// <summary>
876879
/// If set, this object is used to use it's fields, properties and methods as global variables and functions
877880
/// </summary>
878-
public object Context { get; set; }
881+
public object Context
882+
{
883+
get { return context; }
884+
set
885+
{
886+
context = value;
887+
defaultVariables["this"] = context;
888+
}
889+
}
879890

880891
private IDictionary<string, object> variables = new Dictionary<string, object>(StringComparer.Ordinal);
881892

0 commit comments

Comments
 (0)