Skip to content

Commit 05755bb

Browse files
committed
#95 part 2' Cast in nested type ok + Cast int to enum OK
1 parent e348fa7 commit 05755bb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,13 @@ ExpressionEvaluator evaluatorForMethodArgs()
25132513
.SetCategory("Bug resolution")
25142514
.SetCategory("NestedType");
25152515

2516+
yield return new TestCaseData(new ExpressionEvaluator()
2517+
, "Environment.GetFolderPath((Environment.SpecialFolder)5)"
2518+
, null)
2519+
.Returns(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
2520+
.SetCategory("Bug resolution")
2521+
.SetCategory("NestedType");
2522+
25162523
yield return new TestCaseData(new ExpressionEvaluator()
25172524
, "new CodingSeb.ExpressionEvaluator.Tests.OtherNamespace.ClassInOtherNameSpace1.ANestedClass().Value1"
25182525
, null)

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public partial class ExpressionEvaluator
5656
protected static readonly Regex functionArgKeywordsRegex = new Regex(@"^\s*(?<keyword>out|ref|in)\s+((?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)\s+(?=[\p{L}_]))?(?<toEval>(?<varName>[\p{L}_](?>[\p{L}_0-9]*))\s*(=.*)?)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
5757

5858
protected static readonly Regex instanceCreationWithNewKeywordRegex = new Regex(@"^new(?>\s*)((?<isAnonymous>[{{])|((?<name>[\p{L}_][\p{L}_0-9\.]*)(?>\s*)(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?>\s*)((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
59-
protected string CastRegexPattern { get { return @"^\((?>\s*)(?<typeName>[\p{L}_][\p{L}_0-9" + (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) + @"\[\]<>]*[?]?)(?>\s*)\)"; } }
59+
protected string CastRegexPattern { get { return @"^\((?>\s*)(?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)(?>\s*)\)"; } }
6060

6161
// To remove comments in scripts based on https://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689
6262
protected const string blockComments = @"/\*(.*?)\*/";
@@ -1641,7 +1641,8 @@ protected virtual bool EvaluateCast(string expression, Stack<object> stack, ref
16411641
{
16421642
string typeName = castMatch.Groups["typeName"].Value;
16431643

1644-
Type type = GetTypeByFriendlyName(typeName);
1644+
int typeIndex = 0;
1645+
Type type = EvaluateType(typeName, ref typeIndex);
16451646

16461647
if (type != null)
16471648
{
@@ -4006,6 +4007,10 @@ protected static object ChangeType(object value, Type conversionType)
40064007
NullableConverter nullableConverter = new NullableConverter(conversionType);
40074008
conversionType = nullableConverter.UnderlyingType;
40084009
}
4010+
if(conversionType.IsEnum)
4011+
{
4012+
return Enum.ToObject(conversionType, value);
4013+
}
40094014
return Convert.ChangeType(value, conversionType);
40104015
}
40114016

0 commit comments

Comments
 (0)