Example #1
0
 private static Expression CreateValueExpression(Type targetType, object value, CultureInfo culture)
 {
     if (targetType != typeof(string) && (!targetType.GetTypeInfo().IsValueType || TypeExtensions.IsNullableType(targetType)) &&
         string.Compare(value as string, "null", StringComparison.OrdinalIgnoreCase) == 0)
     {
         value = null;
     }
     if (value != null)
     {
         Type nonNullableType = TypeExtensions.GetNonNullableType(targetType);
         if (value.GetType() != nonNullableType)
         {
             if (nonNullableType.GetTypeInfo().IsEnum)
             {
                 value = Enum.Parse(nonNullableType, value.ToString(), true);
             }
             else if (nonNullableType == typeof(Guid))
             {
                 value = new Guid(value.ToString());
             }
             else if (value is IConvertible)
             {
                 value = Convert.ChangeType(value, nonNullableType, culture);
             }
         }
     }
     return(CreateConstantExpression(value));
 }
Example #2
0
        protected virtual Expression CreateMemberExpression()
        {
            Type memberType = FilterDescriptor.MemberType;
            MemberAccessExpressionBuilderBase expressionBuilderBase =
                ExpressionBuilderFactory.MemberAccess(ParameterExpression.Type, memberType, FilterDescriptor.Member);

            expressionBuilderBase.Options.CopyFrom(Options);
            expressionBuilderBase.ParameterExpression = ParameterExpression;
            Expression expression = expressionBuilderBase.CreateMemberAccessExpression();

            if (memberType != null &&
                TypeExtensions.GetNonNullableType(expression.Type) != TypeExtensions.GetNonNullableType(memberType))
            {
                expression = Expression.Convert(expression, memberType);
            }
            return(expression);
        }