using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using Luticate2.Auth.Utils.Dbo.Result; using Luticate2.Auth.Utils.Interfaces; namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor { public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity { public LuResult 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 LuResult.Ok(lambda); } public LuResult GetMethodValueExpression(MethodInfo methodInfo, ILuObjectConverterDescriptorOptions options) { var lambdaParams = new List { 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 LuResult.Ok(lambda); } } }