1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using Luticate2.Auth.Utils.Interfaces;
-
- namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
- {
- public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity
- {
- public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, ILuObjectConverterDescriptorOptions options)
- {
- var isStatic = false;
- if (memberInfo is FieldInfo fieldInfo)
- {
- isStatic = fieldInfo.IsStatic;
- }
- else if (memberInfo is PropertyInfo propertyInfo)
- {
- isStatic = propertyInfo.GetAccessors(true)[0].IsStatic;
- }
- var param = Expression.Parameter(memberInfo.DeclaringType);
- var memberExpression = Expression.MakeMemberAccess(isStatic ? null : param, memberInfo);
- var lambda = Expression.Lambda(memberExpression, param);
- return lambda;
- }
-
- public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options)
- {
- var lambdaParams = new List<ParameterExpression>
- {
- Expression.Parameter(methodInfo.DeclaringType) // TODO what if DeclaringType is static class
- };
- var methodParams = methodInfo.GetParameters().Select(x => Expression.Parameter(x.ParameterType)).ToList();
- lambdaParams.AddRange(methodParams);
- var methodCallExpression = Expression.Call(methodInfo.IsStatic ? null : lambdaParams[0], methodInfo, methodParams);
- var lambda = Expression.Lambda(methodCallExpression, lambdaParams);
- return lambda;
- }
- }
- }
|