Example #1
0
 private Infrastructure.OeShadowPropertyInfo CreateShadowProperty(IPropertyBase efProperty)
 {
     if (!_shadowProperties.TryGetValue(efProperty, out Infrastructure.OeShadowPropertyInfo shadowProperty))
     {
         shadowProperty = new Infrastructure.OeShadowPropertyInfo(efProperty.DeclaringType.ClrType, efProperty.ClrType, efProperty.Name);
         _shadowProperties.Add(efProperty, shadowProperty);
     }
     return(shadowProperty);
 }
        protected override Expression VisitMember(MemberExpression node)
        {
            Expression   expression = base.Visit(node.Expression);
            PropertyInfo property   = node.Member as PropertyInfo;

            if (property != null && expression.Type == node.Expression.Type)
            {
                if (property.PropertyType == typeof(DateTimeOffset))
                {
                    if (property.DeclaringType == typeof(DateTimeOffset?) && property.Name == nameof(Nullable <DateTimeOffset> .Value))
                    {
                        return(Expression.MakeMemberAccess(expression, expression.Type.GetMember(node.Member.Name).Single()));
                    }

                    var propertyInfo = new Infrastructure.OeShadowPropertyInfo(property.DeclaringType, typeof(DateTime), property.Name);
                    return(Expression.Property(expression, propertyInfo));
                }
                if (property.PropertyType == typeof(DateTimeOffset?))
                {
                    var propertyInfo = new Infrastructure.OeShadowPropertyInfo(property.DeclaringType, typeof(DateTime?), property.Name);
                    return(Expression.Property(expression, propertyInfo));
                }
            }

            if (expression != node.Expression)
            {
                if (IsAnonymousType(node.Expression.Type))
                {
                    node = MapPropertyAnonymousTypeToTuple(expression, (PropertyInfo)node.Member);
                }
                else
                {
                    node = Expression.MakeMemberAccess(expression, expression.Type.GetMember(node.Member.Name).Single());
                }
            }

            if (property != null && property.PropertyType == typeof(Decimal))
            {
                return(Expression.Convert(node, typeof(double)));
            }
            if (property != null && property.PropertyType == typeof(Decimal?))
            {
                return(Expression.Convert(node, typeof(double?)));
            }

            return(node);

            MemberExpression MapPropertyAnonymousTypeToTuple(Expression tupleInstance, PropertyInfo anonymousProperty)
            {
                Type tupleType = expression.Type;

                PropertyInfo[] anonymousProperties = anonymousProperty.DeclaringType.GetProperties();
                int            index = Array.IndexOf(anonymousProperties, anonymousProperty);

                MemberExpression memberAccess = null;

                while (OeExpressionHelper.IsTupleType(tupleType))
                {
                    int          offset        = index > 7 ? 7 : index;
                    PropertyInfo tupleProperty = tupleType.GetProperties()[offset];
                    if (memberAccess == null)
                    {
                        memberAccess = Expression.Property(tupleInstance, tupleProperty);
                    }
                    else
                    {
                        memberAccess = Expression.Property(memberAccess, tupleProperty);
                    }
                    tupleType = tupleProperty.PropertyType;

                    index -= offset;
                }
                return(memberAccess);
            }
        }