网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;

namespace Dpz.Core.Infrastructure.ExpressTreeQuery.ComparisonStrategy;

public class EqualComparison : IComparisonStrategy
{
    public Expression BuildExpression(Expression memberExpression, object? value)
    {
        return Expression.Equal(memberExpression, Expression.Constant(value));
    }
}

public class NotEqualComparison : IComparisonStrategy
{
    public Expression BuildExpression(Expression memberExpression, object? value)
    {
        return Expression.NotEqual(memberExpression, Expression.Constant(value));
    }
}

public class GreaterThanComparison : IComparisonStrategy
{
    public Expression BuildExpression(Expression memberExpression, object? value)
    {
        return Expression.GreaterThan(memberExpression, Expression.Constant(value));
    }
}

public class ContainsComparison : IComparisonStrategy
{
    public Expression BuildExpression(Expression memberExpression, object? value)
    {
        if (value is string stringValue)
        {
            var containsMethod = typeof(string).GetMethod("Contains", [typeof(string)]);
            if (containsMethod != null)
            {
                return Expression.Call(
                    memberExpression,
                    containsMethod,
                    Expression.Constant(stringValue)
                );
            }
        }

        if (value is IEnumerable enumerable)
        {
            var containsMethod = typeof(Enumerable)
                .GetMethods()
                .FirstOrDefault(m => m.Name == "Contains" && m.GetParameters().Length == 2)
                ?.MakeGenericMethod(memberExpression.Type);
            if (containsMethod != null)
            {
                return Expression.Call(
                    containsMethod,
                    Expression.Constant(enumerable),
                    memberExpression
                );
            }
        }

        return Expression.Constant(false);
    }
}
loading