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.

LuObjectConverterDescriptorIdentity.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using Luticate2.Auth.Utils.Interfaces;
  6. namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
  7. {
  8. public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity
  9. {
  10. public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
  11. {
  12. var isStatic = false;
  13. if (memberInfo is FieldInfo fieldInfo)
  14. {
  15. isStatic = fieldInfo.IsStatic;
  16. }
  17. else if (memberInfo is PropertyInfo propertyInfo)
  18. {
  19. isStatic = propertyInfo.GetAccessors(true)[0].IsStatic;
  20. }
  21. var param = Expression.Parameter(memberInfo.DeclaringType);
  22. var memberExpression = Expression.MakeMemberAccess(isStatic ? null : param, memberInfo);
  23. var lambda = Expression.Lambda(memberExpression, param);
  24. return lambda;
  25. }
  26. public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
  27. {
  28. var lambdaParams = new List<ParameterExpression>
  29. {
  30. Expression.Parameter(methodInfo.DeclaringType) // TODO what if DeclaringType is static class
  31. };
  32. var methodParams = methodInfo.GetParameters().Select(x => Expression.Parameter(x.ParameterType)).ToList();
  33. lambdaParams.AddRange(methodParams);
  34. var methodCallExpression = Expression.Call(methodInfo.IsStatic ? null : lambdaParams[0], methodInfo, methodParams);
  35. var lambda = Expression.Lambda(methodCallExpression, lambdaParams);
  36. return lambda;
  37. }
  38. }
  39. }