您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuExpressionConverter.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using Luticate2.Auth.Business.Fields.DMEC;
  6. using Luticate2.Auth.Dbo;
  7. using Luticate2.Auth.Dbo.Result;
  8. using Luticate2.Auth.Interfaces;
  9. using Microsoft.EntityFrameworkCore.Internal;
  10. namespace Luticate2.Auth.Business.Fields
  11. {
  12. public class LuExpressionConverter<TTypeFrom, TTypeTo> : ILuExpressionConverter<TTypeFrom, TTypeTo>
  13. {
  14. protected IServiceProvider ServiceProvider { get; }
  15. public LuExpressionConverter(IServiceProvider serviceProvider)
  16. {
  17. ServiceProvider = serviceProvider;
  18. }
  19. public virtual LuResult<Expression> ConvertExpression(Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
  20. {
  21. if (!expressions.Any())
  22. {
  23. return LuResult<Expression>.Ok(baseExpression);
  24. }
  25. var expression = expressions.Peek();
  26. if (expression is ParameterExpression parameterExpression)
  27. {
  28. if (parameterExpression.Type != typeof(TTypeFrom))
  29. {
  30. return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad parameter expression type");
  31. }
  32. expressions.Pop();
  33. return ConvertExpression(expressions, options.Parameter, options);
  34. }
  35. else if (expression is MemberExpression memberExpression)
  36. {
  37. if (memberExpression.Member.DeclaringType != typeof(TTypeFrom))
  38. {
  39. return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad member expression type");
  40. }
  41. }
  42. else if (expression is MethodCallExpression methodCallExpression)
  43. {
  44. if (methodCallExpression.Object != null && methodCallExpression.Object.Type != typeof(TTypeFrom))
  45. {
  46. return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad method call expression type");
  47. }
  48. }
  49. var convertResult = ConvertInternal(expressions, baseExpression, options);
  50. return convertResult;
  51. }
  52. protected LuResult<Expression> ConvertOther(Type typeFrom, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
  53. {
  54. var type = typeof(ILuExpressionConverter<>);
  55. var gtype = type.MakeGenericType(typeFrom);
  56. var converter = (ILuExpressionConverter) ServiceProvider.GetService(gtype);
  57. if (converter == null)
  58. {
  59. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(), $"Could not get service: {gtype}>");
  60. }
  61. return converter.ConvertExpression(expressions, baseExpression, options);
  62. }
  63. protected bool MemberMatch<TTypeFromProperty>(Expression<Func<TTypeFrom, TTypeFromProperty>> member, MemberExpression memberExpression)
  64. {
  65. return LuExpressionUtils.GetSingleMemberFromExpression(member) == memberExpression.Member;
  66. }
  67. protected LuResult<Expression> MakeMemberAccess<TTypeToProperty>(Expression<Func<TTypeTo, TTypeToProperty>> expression, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
  68. {
  69. var exp = expressions.Pop();
  70. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression(expression);
  71. var newExpression = Expression.MakeMemberAccess(baseExpression, memberInfo);
  72. var convertResult = ConvertOther(exp.Type, expressions, newExpression, options);
  73. return convertResult;
  74. }
  75. // protected LuResult<Expression> MakeMethodCall<TTypeToProperty>(Expression<Func<TTypeTo, TTypeToProperty>> expression, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
  76. // {
  77. // var exp = expressions.Pop();
  78. // }
  79. protected virtual LuResult<Expression> ConvertInternal(Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
  80. {
  81. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(), "Not supported expression");
  82. }
  83. }
  84. }