You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuObjectConverterDescriptor.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using Luticate2.Auth.Utils.Business.Utils;
  7. using Luticate2.Auth.Utils.Interfaces;
  8. namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
  9. {
  10. public class LuObjectConverterDescriptor : ILuObjectConverterDescriptor
  11. {
  12. protected class LuObjectConverterDescriptorMemberInfo
  13. {
  14. public LambdaExpression ValueExpression { get; set; }
  15. }
  16. protected class LuObjectConverterDescriptorMethodInfo
  17. {
  18. public LambdaExpression ValueExpression { get; set; }
  19. }
  20. protected delegate LuObjectConverterDescriptorMethodInfo DynamicMethodConverter(MethodInfo methodInfo, ILuConvertersTypeConverter typeConverter);
  21. private IDictionary<MemberInfo, LuObjectConverterDescriptorMemberInfo> StaticMemberConverters { get; } = new Dictionary<MemberInfo, LuObjectConverterDescriptorMemberInfo>();
  22. private IDictionary<MethodInfo, LuObjectConverterDescriptorMethodInfo> StaticMethodConverters { get; } = new Dictionary<MethodInfo, LuObjectConverterDescriptorMethodInfo>();
  23. private IDictionary<MethodInfo, DynamicMethodConverter> DynamicMethodConverters { get; } = new Dictionary<MethodInfo, DynamicMethodConverter>();
  24. protected void AddStaticMemberConverter(LambdaExpression memberFrom, LambdaExpression valueExpression)
  25. {
  26. // TODO: check memberInfo != null
  27. var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression(memberFrom);
  28. StaticMemberConverters.Add(memberInfo, new LuObjectConverterDescriptorMemberInfo
  29. {
  30. ValueExpression = LuExpressionUtils.GetFromConvert(valueExpression)
  31. });
  32. }
  33. protected void AddStaticMethodConverter(LambdaExpression methodFrom, LambdaExpression valueExpression)
  34. {
  35. // TODO: check methodInfo != null
  36. var methodInfo = LuExpressionUtils.GetSingleMethodFromExpression(methodFrom);
  37. StaticMethodConverters.Add(methodInfo, new LuObjectConverterDescriptorMethodInfo
  38. {
  39. ValueExpression = LuExpressionUtils.GetFromConvert(valueExpression)
  40. });
  41. }
  42. protected void AddDynamicMethodConverter(LambdaExpression methodFrom, DynamicMethodConverter converter)
  43. {
  44. // TODO: check methodInfo != null
  45. var methodInfo = LuExpressionUtils.GetSingleMethodFromExpression(methodFrom);
  46. var genericMethodInfo = methodInfo.GetGenericMethodDefinition();
  47. DynamicMethodConverters.Add(genericMethodInfo, converter);
  48. }
  49. protected void AddDynamicMethodConverterTemplate(LambdaExpression methodFrom)
  50. {
  51. AddDynamicMethodConverter(methodFrom,
  52. (info, converter) =>
  53. {
  54. var genericMethodInfo = info.GetGenericMethodDefinition();
  55. var templateMethodInfo = genericMethodInfo.MakeGenericMethod(info.GetGenericArguments().Select(converter.ConvertType).ToArray());
  56. var lambdaParams = new List<ParameterExpression>
  57. {
  58. Expression.Parameter(templateMethodInfo.DeclaringType) // TODO what if DeclaringType is static class
  59. };
  60. var methodParams = templateMethodInfo.GetParameters().Select(x => Expression.Parameter(x.ParameterType)).ToList();
  61. lambdaParams.AddRange(methodParams);
  62. var methodCallExpression = Expression.Call(templateMethodInfo.IsStatic ? null : lambdaParams[0], templateMethodInfo, methodParams);
  63. var body = LuExpressionUtils.GetFromConvert(methodCallExpression);
  64. var lambda = Expression.Lambda(body, lambdaParams);
  65. return new LuObjectConverterDescriptorMethodInfo
  66. {
  67. ValueExpression = lambda
  68. };
  69. }
  70. );
  71. }
  72. public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
  73. {
  74. if (StaticMemberConverters.ContainsKey(memberInfo))
  75. {
  76. return StaticMemberConverters[memberInfo].ValueExpression;
  77. }
  78. return null;
  79. }
  80. public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
  81. {
  82. if (StaticMethodConverters.ContainsKey(methodInfo))
  83. {
  84. return StaticMethodConverters[methodInfo].ValueExpression;
  85. }
  86. var genericMethodInfo = methodInfo.GetGenericMethodDefinition();
  87. if (DynamicMethodConverters.ContainsKey(genericMethodInfo))
  88. {
  89. var result = DynamicMethodConverters[genericMethodInfo](methodInfo, options.TypeConverter);
  90. if (result != null)
  91. {
  92. return result.ValueExpression;
  93. }
  94. }
  95. return null;
  96. }
  97. }
  98. public class LuObjectConverterDescriptor<TTypeFrom, TTypeTo> : LuObjectConverterDescriptor, ILuObjectConverterDescriptor<TTypeFrom, TTypeTo>
  99. {
  100. protected void AddStaticMemberConverter(Expression<Func<TTypeFrom, object>> memberFrom,
  101. Expression<Func<TTypeTo, object>> valueExpression)
  102. {
  103. AddStaticMemberConverter((LambdaExpression) memberFrom, valueExpression);
  104. }
  105. protected void AddNullMemberConverter<TType>(Expression<Func<TTypeFrom, object>> memberFrom)
  106. {
  107. var nullExp = Expression.Constant(null, typeof(TType));
  108. var param = Expression.Parameter(typeof(TTypeTo));
  109. var lambda = Expression.Lambda(nullExp, param);
  110. AddStaticMemberConverter(memberFrom, lambda);
  111. }
  112. }
  113. }