Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class NullHelper
- {
- public static Func<T, bool> GetNullCheck<T, TProperty>(Expression<Func<T, TProperty>> expression)
- {
- var memberExpressions = GetMembersExpressions(expression.Body);
- var funcs = memberExpressions.Select(exp => ConvertToFunc(exp));
- var funcStack = new Stack<Func<object,object>>(funcs);
- return obj => {
- if (obj == null)
- return false;
- object currentObj = obj;
- foreach (Func<object, object> func in funcStack)
- {
- object memberValue = func(currentObj);
- if (memberValue == null)
- return false;
- currentObj = memberValue;
- }
- return true;
- };
- }
- private static IEnumerable<Expression> GetMembersExpressions(Expression expression)
- {
- yield return expression;
- var leftSide = GetLeftSideOfExpression(expression);
- while (leftSide != null && IsConvertibleToFunc(leftSide))
- {
- yield return leftSide;
- leftSide = GetLeftSideOfExpression(leftSide);
- }
- }
- private static Expression GetLeftSideOfExpression(Expression expression)
- {
- var member = expression as MemberExpression;
- if (member != null)
- return member.Expression;
- var call = expression as MethodCallExpression;
- if (call != null)
- return call.Object;
- return null;
- }
- private static bool IsConvertibleToFunc(Expression expression)
- {
- return expression is MemberExpression || expression is MethodCallExpression;
- }
- private static Func<object, object> ConvertToFunc(Expression expression)
- {
- var memberExpression = expression as MemberExpression;
- if (memberExpression != null)
- {
- var propertyInfo = memberExpression.Member as PropertyInfo;
- if (propertyInfo != null)
- {
- var input = Expression.Parameter(typeof(object), "obj");
- var convertedInput = Expression.Convert(input, propertyInfo.DeclaringType);
- var lambda = Expression.Lambda<Func<object, object>>(
- Expression.Convert(Expression.Property(convertedInput, propertyInfo), typeof(object)),
- input
- );
- return lambda.Compile();
- }
- var fieldInfo = memberExpression.Member as FieldInfo;
- if (fieldInfo != null)
- {
- var input = Expression.Parameter(typeof(object), "obj");
- var convertedInput = Expression.Convert(input, fieldInfo.DeclaringType);
- var lambda = Expression.Lambda<Func<object, object>>(
- Expression.Convert(Expression.Field(convertedInput, fieldInfo), typeof(object)),
- input
- );
- return lambda.Compile();
- }
- }
- var methodCallExpression = expression as MethodCallExpression;
- if (methodCallExpression != null)
- {
- var methodInfo = methodCallExpression.Method;
- var input = Expression.Parameter(typeof(object), "obj");
- var convertedInput = Expression.Convert(input, methodInfo.DeclaringType);
- var lambda = Expression.Lambda<Func<object, object>>(
- Expression.Convert(Expression.Call(convertedInput, methodInfo, methodCallExpression.Arguments.ToArray()), typeof(object)),
- input
- );
- return lambda.Compile();
- }
- throw new ArgumentException("Must be a property, field, or method.", "expression");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement