Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer try to emit a constant expression for generic types with generic parameters #218

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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