SqlSugarDynamicExpressionParser.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace SqlSugar
  7. {
  8. public class SqlSugarDynamicExpressionParser
  9. {
  10. public static LambdaExpression ParseLambda(ParameterExpression[] parameterExpressions, Type type, string sql, object[] objects)
  11. {
  12. if (StaticConfig.DynamicExpressionParserType == null)
  13. {
  14. Check.ExceptionEasy("Please at program startup assignment: StaticConfig DynamicExpressionParserType = typeof (DynamicExpressionParser); NUGET is required to install Dynamic.Core", "请在程序启动时赋值: StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); 需要NUGET安装 Dynamic.Core");
  15. }
  16. if (StaticConfig.DynamicExpressionParsingConfig != null)
  17. {
  18. // 查找 ParseLambda 方法
  19. MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType
  20. .GetMyMethod("ParseLambda", 5, StaticConfig.DynamicExpressionParsingConfig.GetType(), typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[]));
  21. if (parseLambdaMethod == null)
  22. {
  23. throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType.");
  24. }
  25. // 调用 ParseLambda 方法来解析 Lambda 表达式
  26. var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { StaticConfig.DynamicExpressionParsingConfig, parameterExpressions, type, sql, objects });
  27. return lambda;
  28. }
  29. else
  30. {
  31. // 查找 ParseLambda 方法
  32. MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType
  33. .GetMyMethod("ParseLambda",4, typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[]));
  34. if (parseLambdaMethod == null)
  35. {
  36. throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType.");
  37. }
  38. // 调用 ParseLambda 方法来解析 Lambda 表达式
  39. var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { parameterExpressions, type, sql, objects });
  40. return lambda;
  41. }
  42. }
  43. }
  44. }