static bool Compare(object leftOperand, ComparisonCondition operatorType, object rightOperand)
        {
            if (leftOperand != null && rightOperand != null)
            {
                rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName);
            }

            IComparable leftComparableOperand  = leftOperand as IComparable;
            IComparable rightComparableOperand = rightOperand as IComparable;

            if ((leftComparableOperand != null) && (rightComparableOperand != null))
            {
                return(EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand));
            }

            switch (operatorType)
            {
            case ComparisonCondition.Equal:
                return(object.Equals(leftOperand, rightOperand));

            case ComparisonCondition.NotEqual:
                return(!object.Equals(leftOperand, rightOperand));

            case ComparisonCondition.LessThan:
            case ComparisonCondition.LessThanOrEqual:
            case ComparisonCondition.GreaterThan:
            case ComparisonCondition.GreaterThanOrEqual:
            {
                if (leftComparableOperand == null && rightComparableOperand == null)
                {
                    throw new ArgumentException("Invalid operands");
                }
                else if (leftComparableOperand == null)
                {
                    throw new ArgumentException("Invalid left operand");
                }
                else
                {
                    throw new ArgumentException("Invalid right operand");
                }
            }
            }

            return(false);
        }
Beispiel #2
0
        void UpdatePropertyValue(object targetObject)
        {
            Type         targetType   = targetObject.GetType();
            PropertyInfo propertyInfo = targetType.GetRuntimeProperty(PropertyName);

            ValidateProperty(targetType.Name, propertyInfo);

            Exception innerException = null;

            try
            {
                object   result           = null;
                Type     propertyType     = propertyInfo.PropertyType;
                TypeInfo propertyTypeInfo = propertyType.GetTypeInfo();

                if (Value == null)
                {
                    result = propertyTypeInfo.IsValueType ? Activator.CreateInstance(propertyType) : null;
                }
                else if (propertyTypeInfo.IsAssignableFrom(Value.GetType().GetTypeInfo()))
                {
                    result = Value;
                }
                else
                {
                    string valueAsString = Value.ToString();
                    result = propertyTypeInfo.IsEnum ? Enum.Parse(propertyType, valueAsString, false) : TypeConverterHelper.Convert(valueAsString, propertyType.FullName);
                }
                propertyInfo.SetValue(targetObject, result, new object[0]);
            }
            catch (FormatException ex)
            {
                innerException = ex;
            }
            catch (ArgumentException ex)
            {
                innerException = ex;
            }

            if (innerException != null)
            {
                throw new ArgumentException("Cannot set value.", innerException);
            }
        }