Skip to content

Avoid lambda compilation for constant and member access expressions in Criteria #2448

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

Merged
merged 2 commits into from
Jul 28, 2020
Merged
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
28 changes: 22 additions & 6 deletions src/NHibernate/Impl/ExpressionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ private static ICriterion Le(ProjectionInfo property, object value)
/// </summary>
public static object FindValue(Expression expression)
{
if (expression.NodeType == ExpressionType.Constant)
return ((ConstantExpression) expression).Value;

if (expression.NodeType == ExpressionType.MemberAccess)
{
var memberAccess = (MemberExpression) expression;
if (memberAccess.Expression == null || memberAccess.Expression.NodeType == ExpressionType.Constant)
{
var constantValue = ((ConstantExpression) memberAccess.Expression)?.Value;
var member = memberAccess.Member;
switch (member.MemberType)
{
case MemberTypes.Field:
return ((FieldInfo) member).GetValue(constantValue);
case MemberTypes.Property:
return ((PropertyInfo) member).GetValue(constantValue);
}
}
}

var valueExpression = Expression.Lambda(expression).Compile();
object value = valueExpression.DynamicInvoke();
return value;
Expand Down Expand Up @@ -452,16 +472,12 @@ public static DetachedCriteria FindDetachedCriteria(Expression expression)
if (methodCallExpression == null)
throw new ArgumentException("right operand should be detachedQueryInstance.As<T>() - " + expression, nameof(expression));

var criteriaExpression = Expression.Lambda(methodCallExpression.Object).Compile();
QueryOver detachedQuery = (QueryOver)criteriaExpression.DynamicInvoke();
return detachedQuery.DetachedCriteria;
return ((QueryOver) FindValue(methodCallExpression.Object)).DetachedCriteria;
}

private static bool EvaluatesToNull(Expression expression)
{
var valueExpression = Expression.Lambda(expression).Compile();
object value = valueExpression.DynamicInvoke();
return (value == null);
return FindValue(expression) == null;
}

private static System.Type FindMemberType(Expression expression)
Expand Down