in src/Epam.GraphQL/Extensions/ExpressionExtensions.cs [40:94]
public static ExpressionInfo GetExpressionInfo<T1, T2>(this Expression<Func<T1, T2, bool>> expression)
{
if (expression.Body is not BinaryExpression logicalExpression)
{
throw new NotSupportedException("Only binary expressions are supported.");
}
if (logicalExpression.NodeType != ExpressionType.Equal)
{
throw new NotSupportedException("Only binary expressions of type ExpressionType.Equal are supported.");
}
var leftExpression = logicalExpression.Left.RemoveConvert();
var rightExpression = logicalExpression.Right.RemoveConvert();
var leftPropInfo = (leftExpression as MemberExpression)?.Member as PropertyInfo ??
throw new NotSupportedException(
"Left operand does not refer to a property of the first argument of expression.");
var rightPropInfo = (rightExpression as MemberExpression)?.Member as PropertyInfo ??
throw new NotSupportedException(
"Right operand does not refer to a property of the second argument of expression.");
var leftParameter = GetParameterOfType<T1>(leftExpression);
if (leftParameter == null)
{
throw new NotSupportedException(
"Left operand does not refer to a property of the first argument of expression.");
}
var rightParameter = GetParameterOfType<T2>(rightExpression);
if (rightParameter == null)
{
throw new NotSupportedException(
"Right operand does not refer to a property of the second argument of expression.");
}
if (leftParameter == expression.Parameters[1] && rightParameter == expression.Parameters[0])
{
return new ExpressionInfo
{
RightPropertyInfo = leftPropInfo,
LeftPropertyInfo = rightPropInfo,
RightExpression = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(T1), leftPropInfo.PropertyType), leftExpression, leftParameter),
LeftExpression = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(T2), rightPropInfo.PropertyType), rightExpression, rightParameter),
};
}
return new ExpressionInfo
{
LeftPropertyInfo = leftPropInfo,
RightPropertyInfo = rightPropInfo,
LeftExpression = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(T1), leftPropInfo.PropertyType), leftExpression, leftParameter),
RightExpression = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(T2), rightPropInfo.PropertyType), rightExpression, rightParameter),
};
}