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.

LuExpressionUtils.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Dbo;
  7. using Luticate2.Auth.Dbo.Result;
  8. namespace Luticate2.Auth.Business
  9. {
  10. public static class LuExpressionUtils
  11. {
  12. public static Expression GetFromConvert<TType1, TType2>(Expression<Func<TType1, TType2>> exp)
  13. {
  14. Expression operand;
  15. if ((exp.Body.NodeType == ExpressionType.Convert ||
  16. exp.Body.NodeType == ExpressionType.ConvertChecked)
  17. && exp.Body is UnaryExpression)
  18. {
  19. operand = ((UnaryExpression) exp.Body).Operand;
  20. }
  21. else
  22. {
  23. operand = exp.Body;
  24. }
  25. return operand;
  26. }
  27. public static Expression<Func<TType1, TType3>> AddConvert<TType1, TType2, TType3>(Expression<Func<TType1, TType2>> exp)
  28. {
  29. var converted = Expression.Convert(exp, typeof(object));
  30. var expLambda = Expression.Lambda<Func<TType1, TType3>>(converted, exp.Parameters);
  31. return expLambda;
  32. }
  33. public static LuResult<TProperty> SetValueFromExpression<TDbo, TProperty>(Expression<Func<TDbo, TProperty>> property,
  34. TDbo dboTo, TProperty value)
  35. {
  36. var memberInfo = GetSingleMemberFromExpression(property);
  37. if (memberInfo != null)
  38. {
  39. switch (memberInfo.MemberType)
  40. {
  41. case MemberTypes.Field:
  42. ((FieldInfo) memberInfo).SetValue(dboTo, value);
  43. return LuResult<TProperty>.Ok(value);
  44. case MemberTypes.Property:
  45. ((PropertyInfo) memberInfo).SetValue(dboTo, value);
  46. return LuResult<TProperty>.Ok(value);
  47. default:
  48. return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(),
  49. $"Bad MemberType: {memberInfo.MemberType}");
  50. }
  51. }
  52. return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(), "Bad member expression");
  53. }
  54. public static LuResult<TProperty> GetValueFromExpression<TDbo, TProperty>(Expression<Func<TDbo, TProperty>> property,
  55. TDbo dboTo)
  56. {
  57. var memberInfo = GetSingleMemberFromExpression(property);
  58. if (memberInfo != null)
  59. {
  60. TProperty value;
  61. switch (memberInfo.MemberType)
  62. {
  63. case MemberTypes.Field:
  64. value = (TProperty) ((FieldInfo) memberInfo).GetValue(dboTo);
  65. return LuResult<TProperty>.Ok(value);
  66. case MemberTypes.Property:
  67. value = (TProperty) ((PropertyInfo) memberInfo).GetValue(dboTo);
  68. return LuResult<TProperty>.Ok(value);
  69. default:
  70. return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(),
  71. $"Bad MemberType: {memberInfo.MemberType}");
  72. }
  73. }
  74. return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(), "Bad member expression");
  75. }
  76. public static MemberInfo GetSingleMemberFromExpression<TTypeTo, TProperty>(Expression<Func<TTypeTo, TProperty>> property)
  77. {
  78. var memberExpression = GetFromConvert(property) as MemberExpression;
  79. if (memberExpression != null && memberExpression.Expression == property.Parameters.First())
  80. {
  81. return memberExpression.Member;
  82. }
  83. return null;
  84. }
  85. private static bool GetMembersFromExpression<TTypeTo, TProperty>(
  86. Expression<Func<TTypeTo, TProperty>> property, IList<MemberInfo> memberInfos)
  87. {
  88. MemberExpression memberExpression;
  89. if ((property.Body.NodeType == ExpressionType.Convert ||
  90. property.Body.NodeType == ExpressionType.ConvertChecked)
  91. && property.Body is UnaryExpression)
  92. {
  93. memberExpression = ((UnaryExpression) property.Body).Operand as MemberExpression;
  94. }
  95. else
  96. {
  97. memberExpression = property.Body as MemberExpression;
  98. }
  99. if (memberExpression != null)
  100. {
  101. if (memberExpression.Expression == property.Parameters.First())
  102. {
  103. memberInfos.Add(memberExpression.Member);
  104. return true;
  105. }
  106. else if (memberExpression.Expression is MemberExpression)
  107. {
  108. var exp = Expression.Lambda<Func<TTypeTo, object>>(memberExpression.Expression, property.Parameters);
  109. var res = GetMembersFromExpression(exp, memberInfos);
  110. if (res)
  111. {
  112. memberInfos.Add(memberExpression.Member);
  113. }
  114. return res;
  115. }
  116. }
  117. return false;
  118. }
  119. public static IList<MemberInfo> GetMembersFromExpression<TTypeTo, TProperty>(Expression<Func<TTypeTo, TProperty>> property)
  120. {
  121. var list = new List<MemberInfo>();
  122. if (GetMembersFromExpression(property, list))
  123. {
  124. return list;
  125. }
  126. return null;
  127. }
  128. }
  129. }