Skip to content

Commit

Permalink
No longer try to emit a constant expression for generic types with ge…
Browse files Browse the repository at this point in the history
…neric parameters (e.g. List<T>). (#218)

Fixes #217
  • Loading branch information
metoule committed Dec 20, 2021
1 parent 048ce1d commit 08404b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,8 @@ private static Expression PromoteExpression(Expression expr, Type type, bool exa
var ce = (ConstantExpression)expr;
if (ce == ParserConstants.NullLiteralExpression)
{
if (type.ContainsGenericParameters)
return null;
if (!type.IsValueType || IsNullableType(type))
return Expression.Constant(null, type);
}
Expand Down
17 changes: 17 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,29 @@ public void GitHub_Issue_205()
Assert.AreEqual(-1, interpreter.Eval("(date1 - date2)?.Days"));
}

[Test]
public void GitHub_Issue_217()
{
var target = new Interpreter();
target.Reference(typeof(Utils));
target.Reference(typeof(IEnumerable<>));

Assert.AreEqual(1, Utils.Any((IEnumerable<object>)null));
Assert.AreEqual(2, Utils.Any((IEnumerable)null));
Assert.AreEqual(2, Utils.Any(null));
Assert.AreEqual(1, target.Eval("Utils.Any(list)", new Parameter("list", typeof(IEnumerable<object>), null)));
Assert.AreEqual(2, target.Eval("Utils.Any(list)", new Parameter("list", typeof(IEnumerable), null)));
Assert.AreEqual(2, target.Eval("Utils.Any(null)"));
}

public class Utils
{
public static List<T> Array<T>(IEnumerable<T> collection) => new List<T>(collection);
public static List<dynamic> Array(params dynamic[] array) => Array((IEnumerable<dynamic>)array);
public static IEnumerable<dynamic> Select<TSource>(IEnumerable<TSource> collection, string expression) => new List<dynamic>();
public static IEnumerable<dynamic> Select(IEnumerable collection, string expression) => new List<dynamic>();
public static int Any<T>(IEnumerable<T> collection) => 1;
public static int Any(IEnumerable collection) => 2;
}

}
Expand Down

0 comments on commit 08404b3

Please sign in to comment.