diff --git a/src/DynamicExpresso.Core/Parsing/Parser.cs b/src/DynamicExpresso.Core/Parsing/Parser.cs index 71b79bb2..a2fffa6c 100644 --- a/src/DynamicExpresso.Core/Parsing/Parser.cs +++ b/src/DynamicExpresso.Core/Parsing/Parser.cs @@ -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); } diff --git a/test/DynamicExpresso.UnitTest/GithubIssues.cs b/test/DynamicExpresso.UnitTest/GithubIssues.cs index 27ea76a3..c67ed2d6 100644 --- a/test/DynamicExpresso.UnitTest/GithubIssues.cs +++ b/test/DynamicExpresso.UnitTest/GithubIssues.cs @@ -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)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), 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 Array(IEnumerable collection) => new List(collection); public static List Array(params dynamic[] array) => Array((IEnumerable)array); public static IEnumerable Select(IEnumerable collection, string expression) => new List(); public static IEnumerable Select(IEnumerable collection, string expression) => new List(); + public static int Any(IEnumerable collection) => 1; + public static int Any(IEnumerable collection) => 2; } }