Beispiel #1
0
        /// <summary>
        /// Returns a <see cref="string" /> that represents the instance.
        /// <para />
        /// If the <paramref name="instance" /> is <c>null</c>, this method will return "null". This
        /// method is great when the value of a property must be logged.
        /// </summary>
        /// <param name="instance">The instance, can be <c>null</c>.</param>
        /// <param name="cultureInfo">The culture information.</param>
        /// <returns>A <see cref="string" /> that represents the instance.</returns>
        public static string ToString(object instance, CultureInfo cultureInfo)
        {
            if (instance is null)
            {
                return("null");
            }

#if !NETFX_CORE
            if (instance == DBNull.Value)
            {
                return("dbnull");
            }
#endif

            var instanceType = instance.GetType();
            if (instanceType == typeof(DateTime) || instanceType == typeof(DateTime?))
            {
                return(((DateTime)instance).ToString(cultureInfo));
            }

#if !NETFX_CORE
            // Note: Not supported on NETFX_CORE, don't enable, really doesn't work. If you need a ToString
            // for a specific string, use a cast like the DateTime about

            // Check if there is a culture specific version
            var toStringMethod = instanceType.GetMethodEx("ToString", TypeArray.From <IFormatProvider>());
            if (toStringMethod != null)
            {
                return((string)toStringMethod.Invoke(instance, new object[] { cultureInfo }));
            }
#endif

            return(instance.ToString());
        }
        private bool TryCreateDataTypeExpressionForEnum(Type propertyType, bool isNullable)
        {
            if (!propertyType.IsEnumEx())
            {
                return(false);
            }

            if (DataTypeExpression is null)
            {
                return(true);
            }

            var enumExpressionGenericType = typeof(EnumExpression <>).MakeGenericType(propertyType);

            if (enumExpressionGenericType.IsAssignableFromEx(DataTypeExpression.GetType()) &&
                (DataTypeExpression as NullableDataTypeExpression)?.IsNullable == isNullable)
            {
                return(true);
            }

            var constructorInfo = enumExpressionGenericType.GetConstructorEx(TypeArray.From <bool>());

            DataTypeExpression = (DataTypeExpression)constructorInfo?.Invoke(new object[] { isNullable });

            return(true);
        }
        /// <summary>
        /// Determines what value this converter should return.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <returns>
        /// <c>true</c> if the specified value is visible; otherwise, <c>false</c>.
        /// </returns>
        protected override bool IsVisible(object value, Type targetType, object parameter)
        {
            if (value == null)
            {
                return(false);
            }

            var enumType = value.GetType();

            if (!enumType.IsEnumEx())
            {
                return(false);
            }

            var stringParameter = parameter as string;

            if (string.IsNullOrWhiteSpace(stringParameter))
            {
                return(false);
            }

            var invert = false;

            if (stringParameter.StartsWith("!"))
            {
                invert          = true;
                stringParameter = stringParameter.Substring(1);
            }

            var genericEnumType = typeof(Enum <>).MakeGenericType(enumType);
            var bindingFlags    = BindingFlags.Public | BindingFlags.Static;

            var parseMethod = genericEnumType.GetMethodEx("Parse", TypeArray.From <string, bool>(), bindingFlags);

            var allowedEnumValues = stringParameter.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries);

            foreach (var allowedEnumValueAsString in allowedEnumValues)
            {
                var parameters = new object[] { allowedEnumValueAsString, true };

                try
                {
                    var allowedEnumValue = parseMethod.Invoke(null, parameters);
                    if (value.Equals(allowedEnumValue))
                    {
                        return(!invert);
                    }
                }
                catch (Exception)
                {
                    // Next!
                }
            }

            return(invert);
        }
        /// <summary>
        /// Gets the <c>Parse(string, IFormatProvider)</c> method.
        /// </summary>
        /// <param name="memberType">Type of the member.</param>
        /// <returns></returns>
        protected virtual MethodInfo GetObjectParseMethod(Type memberType)
        {
            var parseMethod = _parseMethodCache.GetFromCacheOrFetch(memberType, () =>
            {
                var method = memberType.GetMethodEx("Parse", TypeArray.From <string, IFormatProvider>(), BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                return(method);
            });

            return(parseMethod);
        }
        /// <summary>
        /// Gets the <c>ToString(IFormatProvider)</c> method.
        /// </summary>
        /// <param name="memberType">Type of the member.</param>
        /// <returns></returns>
        protected virtual MethodInfo GetObjectToStringMethod(Type memberType)
        {
            var toStringMethod = _toStringMethodCache.GetFromCacheOrFetch(memberType, () =>
            {
                var method = memberType.GetMethodEx("ToString", TypeArray.From <IFormatProvider>(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                return(method);
            });

            return(toStringMethod);
        }
Beispiel #6
0
        private bool IsTypeBinarySerializable(Type type)
        {
            if (type == null)
            {
                return(false);
            }

            return(TypeBinarySerializableCache.GetFromCacheOrFetch(type, () => type.GetConstructor(TypeArray.From <SerializationInfo, StreamingContext>()) != null));
        }
        //// TODO: Try to combine expression builders as much as possible.
        private static Expression BuildStringExpression(Condition condition, Expression propertyExpression,
                                                        Expression valueExpression)
        {
            switch (condition)
            {
            case Condition.Contains:
                return(Expression.AndAlso(Expression.Not(Expression.Call(typeof(string).GetMethod("IsNullOrEmpty", TypeArray.From <string>()), propertyExpression)),
                                          Expression.Call(propertyExpression, typeof(string).GetMethod("Contains", TypeArray.From <string>()),
                                                          valueExpression)));

            case Condition.DoesNotContain:
                return(Expression.Not(BuildStringExpression(Condition.Contains, propertyExpression, valueExpression)));

            case Condition.StartsWith:
                return(Expression.AndAlso(Expression.Not(Expression.Call(typeof(string).GetMethod("IsNullOrEmpty", TypeArray.From <string>()), propertyExpression)),
                                          Expression.Call(propertyExpression, typeof(string).GetMethod("StartsWith", TypeArray.From <string>()),
                                                          valueExpression)));

            case Condition.DoesNotStartWith:
                return(Expression.Not(BuildStringExpression(Condition.StartsWith, propertyExpression, valueExpression)));

            case Condition.EndsWith:
                return(Expression.AndAlso(Expression.Not(Expression.Call(typeof(string).GetMethod("IsNullOrEmpty", TypeArray.From <string>()), propertyExpression)),
                                          Expression.Call(propertyExpression, typeof(string).GetMethod("EndsWith", TypeArray.From <string>()),
                                                          valueExpression)));

            case Condition.DoesNotEndWith:
                return(Expression.Not(BuildStringExpression(Condition.EndsWith, propertyExpression, valueExpression)));

            case Condition.EqualTo:
                return(Expression.AndAlso(Expression.Not(Expression.Call(typeof(string).GetMethod("IsNullOrEmpty", TypeArray.From <string>()), propertyExpression)),
                                          Expression.Equal(propertyExpression, valueExpression)));

            case Condition.NotEqualTo:
                return(Expression.Not(BuildStringExpression(Condition.EqualTo, propertyExpression, valueExpression)));

            case Condition.GreaterThan:
                return(Expression.GreaterThan(Expression.Call(
                                                  typeof(string).GetMethod("Compare", TypeArray.From <string, string, StringComparison>()),
                                                  propertyExpression, valueExpression, Expression.Constant(StringComparison.OrdinalIgnoreCase)),
                                              Expression.Constant(0)));

            case Condition.GreaterThanOrEqualTo:
                return(Expression.GreaterThanOrEqual(
                           Expression.Call(
                               typeof(string).GetMethod("Compare", TypeArray.From <string, string, StringComparison>()),
                               propertyExpression, valueExpression, Expression.Constant(StringComparison.OrdinalIgnoreCase)), Expression.Constant(0)));

            case Condition.LessThan:
                return(Expression.LessThan(Expression.Call(
                                               typeof(string).GetMethod("Compare", TypeArray.From <string, string, StringComparison>()),
                                               propertyExpression, valueExpression, Expression.Constant(StringComparison.OrdinalIgnoreCase)),
                                           Expression.Constant(0)));

            case Condition.LessThanOrEqualTo:
                return(Expression.LessThanOrEqual(Expression.Call(
                                                      typeof(string).GetMethod("Compare", TypeArray.From <string, string, StringComparison>()),
                                                      propertyExpression, valueExpression, Expression.Constant(StringComparison.OrdinalIgnoreCase)),
                                                  Expression.Constant(0)));

            case Condition.IsNull:
                return(Expression.Equal(propertyExpression, Expression.Constant(null)));

            case Condition.NotIsNull:
                return(Expression.Not(Expression.Equal(propertyExpression, Expression.Constant(null))));

            case Condition.IsEmpty:
                return(Expression.Equal(propertyExpression, Expression.Constant(string.Empty)));

            case Condition.NotIsEmpty:
                return(Expression.Not(Expression.Equal(propertyExpression, Expression.Constant(string.Empty))));

            default:
                throw new NotSupportedException(string.Format(
                                                    LanguageHelper.GetString("FilterBuilder_Exception_Message_ConditionIsNotSupported_Pattern"),
                                                    condition));
            }
        }