Exemplo n.º 1
0
        private static bool Compare(object leftOperand, ComparisonConditionType 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(DataTriggerBehavior.EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand));
            }

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

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

            case ComparisonConditionType.LessThan:
            case ComparisonConditionType.LessThanOrEqual:
            case ComparisonConditionType.GreaterThan:
            case ComparisonConditionType.GreaterThanOrEqual:
            {
                if (leftComparableOperand == null && rightComparableOperand == null)
                {
                    throw new ArgumentException(string.Format(
                                                    CultureInfo.CurrentCulture,
                                                    ResourceHelper.InvalidOperands,
                                                    leftOperand != null ? leftOperand.GetType().Name : "null",
                                                    rightOperand != null ? rightOperand.GetType().Name : "null",
                                                    operatorType.ToString()));
                }
                else if (leftComparableOperand == null)
                {
                    throw new ArgumentException(string.Format(
                                                    CultureInfo.CurrentCulture,
                                                    ResourceHelper.InvalidLeftOperand,
                                                    leftOperand != null ? leftOperand.GetType().Name : "null",
                                                    operatorType.ToString()));
                }
                else
                {
                    throw new ArgumentException(string.Format(
                                                    CultureInfo.CurrentCulture,
                                                    ResourceHelper.InvalidRightOperand,
                                                    rightOperand != null ? rightOperand.GetType().Name : "null",
                                                    operatorType.ToString()));
                }
            }
            }

            return(false);
        }
Exemplo n.º 2
0
        private void UpdatePropertyValue(object targetObject)
        {
            Type         targetType   = targetObject.GetType();
            PropertyInfo propertyInfo = targetType.GetRuntimeProperty(this.PropertyName.Path);

            this.ValidateProperty(targetType.Name, propertyInfo);

            Exception innerException = null;

            try
            {
                object   result           = null;
                string   valueAsString    = null;
                Type     propertyType     = propertyInfo.PropertyType;
                TypeInfo propertyTypeInfo = propertyType.GetTypeInfo();
                if (this.Value == null)
                {
                    // The result can be null if the type is generic (nullable), or the default value of the type in question
                    result = propertyTypeInfo.IsValueType ? Activator.CreateInstance(propertyType) : null;
                }
                else if (propertyTypeInfo.IsAssignableFrom(this.Value.GetType().GetTypeInfo()))
                {
                    result = this.Value;
                }
                else
                {
                    valueAsString = this.Value.ToString();
                    result        = propertyTypeInfo.IsEnum ? Enum.Parse(propertyType, valueAsString, false) :
                                    TypeConverterHelper.Convert(valueAsString, propertyType.FullName);
                }

                propertyInfo.SetValue(targetObject, result, new object[0]);
            }
            catch (FormatException e)
            {
                innerException = e;
            }
            catch (ArgumentException e)
            {
                innerException = e;
            }

            if (innerException != null)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentCulture,
                                                ResourceHelper.ChangePropertyActionCannotSetValueExceptionMessage,
                                                this.Value != null ? this.Value.GetType().Name : "null",
                                                this.PropertyName,
                                                propertyInfo.PropertyType.Name),
                                            innerException);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts string representation of a value to its object representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="destinationTypeFullName">The full name of the destination type.</param>
        /// <returns>Object representation of the string value.</returns>
        /// <exception cref="ArgumentNullException">destinationTypeFullName cannot be null.</exception>
        public static Object Convert(string value, string destinationTypeFullName)
        {
            if (string.IsNullOrEmpty(destinationTypeFullName))
            {
                throw new ArgumentNullException("destinationTypeFullName");
            }

            string scope = TypeConverterHelper.GetScope(destinationTypeFullName);

            // Value types in the "System" namespace must be special cased due to a bug in the xaml compiler
            if (string.Equals(scope, "System", StringComparison.Ordinal))
            {
                if (string.Equals(destinationTypeFullName, (typeof(string).FullName), StringComparison.Ordinal))
                {
                    return(value);
                }
                else if (string.Equals(destinationTypeFullName, typeof(bool).FullName, StringComparison.Ordinal))
                {
                    return(bool.Parse(value));
                }
                else if (string.Equals(destinationTypeFullName, typeof(int).FullName, StringComparison.Ordinal))
                {
                    return(int.Parse(value, CultureInfo.InvariantCulture));
                }
                else if (string.Equals(destinationTypeFullName, typeof(double).FullName, StringComparison.Ordinal))
                {
                    return(double.Parse(value, CultureInfo.CurrentCulture));
                }
            }

            string type = TypeConverterHelper.GetType(destinationTypeFullName);
            string contentControlXaml = string.Format(CultureInfo.InvariantCulture, TypeConverterHelper.ContentControlFormatString, scope, type, value);

            ContentControl contentControl = XamlReader.Load(contentControlXaml) as ContentControl;

            if (contentControl != null)
            {
                return(contentControl.Content);
            }

            return(null);
        }