Skip to content

Commit 2c6410e

Browse files
committed
Manage Delegates as Properties
1 parent 0b2ab62 commit 2c6410e

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,13 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
10131013
yield return new TestCaseData("Test(5)", delegatesInVariable, true).SetCategory("Delegate as a variable").Returns(null);
10141014

10151015
#endregion
1016+
1017+
#region Delegates as Property of object
1018+
1019+
yield return new TestCaseData("customObject.AddAsDelegate(6, 10)", onInstanceVariables, true).SetCategory("Delegate as a instance Property").Returns(16);
1020+
yield return new TestCaseData("ClassForTest1.AddAsStaticDelegate(6, 10)", onInstanceVariables, true).SetCategory("Delegate as a static Property").Returns(16);
1021+
1022+
#endregion
10161023
}
10171024
}
10181025

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest1.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace CodingSeb.ExpressionEvaluator.Tests
1+
using System;
2+
3+
namespace CodingSeb.ExpressionEvaluator.Tests
24
{
35
public class ClassForTest1
46
{
@@ -17,5 +19,8 @@ public static string StaticStringMethod(string Name)
1719
{
1820
return $"Hello {Name}";
1921
}
22+
23+
public Func<int, int, int> AddAsDelegate { get; set; } = (nb1, nb2) => nb1 + nb2;
24+
public static Func<int, int, int> AddAsStaticDelegate { get; set; } = (nb1, nb2) => nb1 + nb2;
2025
}
2126
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
// -------------------------------------------------------------
2-
// Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
// Author : Coding Seb
4-
// Licence : MIT (https://github.com/codingseb/ExpressionEvaluator/blob/master/LICENSE.md)
5-
// -------------------------------------------------------------
1+
/******************************************************************************************************
2+
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3+
Version : 1.2.1.3
4+
(if last digit not zero version is an intermediate version and can be unstable)
5+
6+
Author : Coding Seb
7+
Licence : MIT (https://github.com/codingseb/ExpressionEvaluator/blob/master/LICENSE.md)
8+
*******************************************************************************************************/
69

710
using System;
811
using System.Collections.Generic;
@@ -1319,6 +1322,11 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
13191322
else
13201323
stack.Push((dictionaryObject[varFuncName] as Delegate).DynamicInvoke(oArgs.ToArray()));
13211324
}
1325+
else if(objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo &&
1326+
(instancePropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || instancePropertyInfo.PropertyType == typeof(Delegate)))
1327+
{
1328+
stack.Push((instancePropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1329+
}
13221330
else
13231331
{
13241332
// if not found try to Find extension methods.
@@ -1338,6 +1346,11 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
13381346
{
13391347
stack.Push(methodInfo.Invoke(obj, oArgs.ToArray()));
13401348
}
1349+
else if (objType.GetProperty(varFuncName, StaticBindingFlag) is PropertyInfo staticPropertyInfo &&
1350+
(staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate)))
1351+
{
1352+
stack.Push((staticPropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1353+
}
13411354
else
13421355
{
13431356
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType.ToString()}] object has no Method named \"{varFuncName}\".");

0 commit comments

Comments
 (0)