Skip to content

Commit 273f070

Browse files
committed
Indexing Improvement
1 parent d49b69e commit 273f070

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,9 @@ public void TypeTesting(string expression, Type type)
869869
[TestCase("List(\"hello\", \"bye\").Select(x => x.ToUpper()).ToList().FluidAdd(\"test\")[2]", ExpectedResult = "test", Category = "Complex expression,Fluid Functions")]
870870
[TestCase("List(\"hello\", \"bye\").Select(x => x.ToUpper()).ToList().FluidAdd(\"test\")[2]", ExpectedResult = "test", Category = "Complex expression,Fluid Functions")]
871871
[TestCase("$\"https://www.google.com/search?q={System.Net.WebUtility.UrlEncode(\"test of request with url encode() ?\")}\"", ExpectedResult = "https://www.google.com/search?q=test+of+request+with+url+encode()+%3F", Category = "Complex expression,Inline namespace")]
872+
[TestCase("new System.Xml.XmlDocument().FluidLoadXml(\"<root><element id='MyElement'>Xml Content</element></root>\").SelectSingleNode(\"//element[@id='MyElement']\").InnerXml", ExpectedResult = "Xml Content", Category = "Complex expression,Inline namespace,Fluid")]
873+
[TestCase("new System.Xml.XmlDocument().FluidLoadXml(\"<root><element id='MyElement'>Xml Content</element></root>\").ChildNodes[0].Name", ExpectedResult = "root", Category = "Complex expression,Inline namespace,Fluid,Custom Indexer")]
874+
872875
#endregion
873876

874877
#endregion

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class ClassForTest1
88

99
public int IntProperty { get; set; } = 25;
1010

11+
12+
1113
public static int StaticIntProperty { get; set; } = 67;
1214

1315
public int Add3To(int value)

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,19 @@ private enum TryBlockEvaluatedState
237237
{
238238
new Dictionary<ExpressionOperator, Func<dynamic, dynamic, object>>()
239239
{
240-
{ExpressionOperator.Indexing, (dynamic left, dynamic right) => {
241-
return left is IDictionary<string,object> dictionaryLeft ? dictionaryLeft[right] : left[right];
240+
{ExpressionOperator.Indexing, (dynamic left, dynamic right) =>
241+
{
242+
Type type = ((object)left).GetType();
243+
244+
if(left is IDictionary<string, object> dictionaryLeft)
245+
return dictionaryLeft[right];
246+
else if(type.GetMethod("Item", new Type[] { ((object)right).GetType() }) is MethodInfo methodInfo)
247+
{
248+
return methodInfo.Invoke(left, new object[] { right });
249+
}
250+
251+
252+
return left[right];
242253
}
243254
},
244255
{ExpressionOperator.IndexingWithNullConditional, (dynamic left, dynamic right) => left is IDictionary<string,object> dictionaryLeft ? dictionaryLeft[right] : left?[right] },

0 commit comments

Comments
 (0)