Skip to content

Commit d30a561

Browse files
committed
Better generic types management in progress
1 parent 59f1583 commit d30a561

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ public void TypeTesting(string expression, Type type)
867867
#region Generic types Management
868868

869869
[TestCase("List(\"Hello\", \"Test\").Cast<string>().ToList<string>().GetType()", ExpectedResult = typeof(List<string>) , Category = "List function, Generics")]
870+
[TestCase("new List<string>().GetType()", ExpectedResult = typeof(List<string>) , Category = "List function, Generics")]
870871

871872
#endregion
872873

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,8 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14161416
|| stack.Peek() is ExpressionOperator))
14171417
{
14181418
string completeName = instanceCreationMatch.Groups["name"].Value;
1419-
Type type = GetTypeByFriendlyName(completeName);
1419+
string genericTypes = instanceCreationMatch.Groups["isgeneric"].Value;
1420+
Type type = GetTypeByFriendlyName(completeName, genericTypes);
14201421

14211422
i += instanceCreationMatch.Length;
14221423

@@ -1839,8 +1840,9 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
18391840
!typeName.EndsWith("?"))
18401841
{
18411842
subIndex += namespaceMatch.Length;
1842-
typeName += $"{namespaceMatch.Value}{((i + subIndex < expr.Length && expr.Substring(i + subIndex)[0] == '?') ? "?" : "") }";
1843-
staticType = GetTypeByFriendlyName(typeName);
1843+
typeName += $".{namespaceMatch.Groups["name"].Value}{((i + subIndex < expr.Length && expr.Substring(i + subIndex)[0] == '?') ? "?" : "") }";
1844+
1845+
staticType = GetTypeByFriendlyName(typeName, namespaceMatch.Groups["isgeneric"].Value);
18441846

18451847
if(staticType != null)
18461848
{
@@ -2658,12 +2660,20 @@ private bool DefaultFunctions(string name, List<string> args, out object result)
26582660
return functionExists;
26592661
}
26602662

2661-
private Type GetTypeByFriendlyName(string typeName)
2663+
private Type GetTypeByFriendlyName(string typeName, string genericTypes = "")
26622664
{
26632665
Type result = null;
26642666
try
26652667
{
2666-
result = Type.GetType(typeName, false, !OptionCaseSensitiveEvaluationActive);
2668+
string formatedGenericTypes = string.Empty;
2669+
2670+
if (!genericTypes.Equals(string.Empty))
2671+
{
2672+
Type[] types = GetConcreteTypes(genericTypes);
2673+
formatedGenericTypes = $"`{types.Length}[{ string.Join(", ", types.Select(type => type.FullName))}]";
2674+
}
2675+
2676+
result = Type.GetType(typeName + formatedGenericTypes, false, !OptionCaseSensitiveEvaluationActive);
26672677

26682678
if (result == null)
26692679
{
@@ -2683,12 +2693,12 @@ private Type GetTypeByFriendlyName(string typeName)
26832693
for (int a = 0; a < Assemblies.Count && result == null; a++)
26842694
{
26852695
if(typeName.Contains("."))
2686-
result = Type.GetType($"{typeName},{Assemblies[a].FullName}", false, !OptionCaseSensitiveEvaluationActive);
2696+
result = Type.GetType($"{typeName}{formatedGenericTypes},{Assemblies[a].FullName}", false, !OptionCaseSensitiveEvaluationActive);
26872697
else
26882698
{
26892699
for (int i = 0; i < Namespaces.Count && result == null; i++)
26902700
{
2691-
result = Type.GetType($"{Namespaces[i]}.{typeName},{Assemblies[a].FullName}", false, !OptionCaseSensitiveEvaluationActive);
2701+
result = Type.GetType($"{Namespaces[i]}.{typeName}{formatedGenericTypes},{Assemblies[a].FullName}", false, !OptionCaseSensitiveEvaluationActive);
26922702
}
26932703
}
26942704
}

0 commit comments

Comments
 (0)