Skip to content

Commit e78f332

Browse files
committed
Correction for #100 (Array types in cast or typeof generate an exception)
1 parent 517fa10 commit e78f332

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,67 @@ ExpressionEvaluator evaluatorForMethodArgs()
26402640

26412641
#endregion
26422642

2643+
#region for issue #100
2644+
2645+
yield return new TestCaseData(new ExpressionEvaluator()
2646+
, "typeof(double[])"
2647+
, null)
2648+
.Returns(typeof(double[]))
2649+
.SetCategory("Bug resolution");
2650+
2651+
yield return new TestCaseData(new ExpressionEvaluator()
2652+
, "typeof(double[ ])"
2653+
, null)
2654+
.Returns(typeof(double[]))
2655+
.SetCategory("Bug resolution");
2656+
2657+
yield return new TestCaseData(new ExpressionEvaluator()
2658+
, "typeof(double[][])"
2659+
, null)
2660+
.Returns(typeof(double[][]))
2661+
.SetCategory("Bug resolution");
2662+
2663+
yield return new TestCaseData(new ExpressionEvaluator()
2664+
, "typeof(double[,])"
2665+
, null)
2666+
.Returns(typeof(double[,]))
2667+
.SetCategory("Bug resolution");
2668+
2669+
yield return new TestCaseData(new ExpressionEvaluator()
2670+
, "typeof(int[])"
2671+
, null)
2672+
.Returns(typeof(int[]))
2673+
.SetCategory("Bug resolution");
2674+
2675+
yield return new TestCaseData(new ExpressionEvaluator()
2676+
, "typeof(Int32[])"
2677+
, null)
2678+
.Returns(typeof(Int32[]))
2679+
.SetCategory("Bug resolution");
2680+
2681+
yield return new TestCaseData(new ExpressionEvaluator()
2682+
, "typeof(string[])"
2683+
, null)
2684+
.Returns(typeof(string[]))
2685+
.SetCategory("Bug resolution");
2686+
2687+
yield return new TestCaseData(new ExpressionEvaluator()
2688+
, "typeof(Regex[])"
2689+
, null)
2690+
.Returns(typeof(Regex[]))
2691+
.SetCategory("Bug resolution");
2692+
2693+
yield return new TestCaseData(new ExpressionEvaluator(new ObjectContainer()
2694+
{
2695+
AnObjectProperty = new double[] {1.1, 2.3, 4.3}
2696+
})
2697+
, "(double[])AnObjectProperty"
2698+
, null)
2699+
.Returns(new double[] { 1.1, 2.3, 4.3 })
2700+
.SetCategory("Bug resolution");
2701+
2702+
#endregion
2703+
26432704
#endregion
26442705
}
26452706
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,18 @@ protected virtual Type EvaluateType(string expression,ref int i, string currentN
25812581
}
25822582
}
25832583

2584+
Match arrayTypeMatch;
2585+
2586+
if(i < expression.Length && (arrayTypeMatch = Regex.Match(expression.Substring(i), @"^(\s*(\[(?>(?>\s+)|[,])*)\])+")).Success)
2587+
{
2588+
Type arrayType = GetTypeByFriendlyName(staticType + arrayTypeMatch.Value);
2589+
if(arrayType != null)
2590+
{
2591+
i += arrayTypeMatch.Length;
2592+
staticType = arrayType;
2593+
}
2594+
}
2595+
25842596
return staticType;
25852597
}
25862598

0 commit comments

Comments
 (0)