private static string BuildMessage(PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType)
        {
            var message = new StringBuilder();

            message.Append("Cannot convert property value of type [");
            if (propertyChangeEventArgs != null && propertyChangeEventArgs.NewValue != null)
            {
                message.Append(propertyChangeEventArgs.NewValue.GetType().FullName);
            }
            else
            {
                message.Append("null");
            }
            message.Append("] to required type [");
            if (requiredType != null)
            {
                message.Append(requiredType.FullName);
            }
            else
            {
                message.Append("null");
            }
            message.Append("] for property '");
            if (propertyChangeEventArgs != null && propertyChangeEventArgs.PropertyName != null)
            {
                message.Append(propertyChangeEventArgs.PropertyName);
            }
            message.Append("'.");
            return(message.ToString());
        }
        /// <summary>
        /// Sets property value, doing any type conversions that are necessary along the way.
        /// </summary>
        /// <param name="context">Context to evaluate expressions against.</param>
        /// <param name="evalContext">Current expression evaluation context.</param>
        /// <param name="newValue">New value for this node.</param>
        private void SetPropertyOrFieldValue(object context, EvaluationContext evalContext, object newValue)
        {
            var isWriteable = _accessor.IsWriteable;
            var targetType = _accessor.TargetType;

            try
            {
                if (!isWriteable)
                {
                    if (!AddToCollections(context, evalContext, newValue))
                    {
                        throw new NotWritablePropertyException(
                            "Can't change the value of the read-only property or field '" + _memberName + "'.");
                    }
                }
                else if (targetType.IsPrimitive && (newValue == null || String.Empty.Equals(newValue)))
                {
                    throw new ArgumentException("Invalid value [" + newValue + "] for property or field '" +
                                                _memberName + "' of primitive type ["
                                                + targetType + "]");
                }
                else if (newValue == null || ObjectUtils.IsAssignable(targetType, newValue)) // targetType.IsAssignableFrom(newValue.GetType())
                {
                    SetPropertyOrFieldValueInternal(context, newValue);
                }
                else if (!RemotingServices.IsTransparentProxy(newValue) &&
                         (newValue is IList || newValue is IDictionary))
                {
                    if (!AddToCollections(context, evalContext, newValue))
                    {
                        var tmpValue =
                            TypeConversionUtils.ConvertValueIfNecessary(targetType, newValue, _memberName);
                        SetPropertyOrFieldValueInternal(context, tmpValue);
                    }
                }
                else
                {
                    var tmpValue = TypeConversionUtils.ConvertValueIfNecessary(targetType, newValue, _memberName);
                    SetPropertyOrFieldValueInternal(context, tmpValue);
                }
            }
            catch (TargetInvocationException ex)
            {
                var propertyChangeEvent =
                    new PropertyChangeEventArgs(_memberName, null, newValue);
                if (ex.GetBaseException() is InvalidCastException)
                {
                    throw new TypeMismatchException(propertyChangeEvent, targetType, ex.GetBaseException());
                }
                throw new MethodInvocationException(ex.GetBaseException(), propertyChangeEvent);
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new FatalReflectionException("Illegal attempt to set property '" + _memberName + "'", ex);
            }
            catch (NotWritablePropertyException)
            {
                throw;
            }
            catch (NotReadablePropertyException)
            {
                throw;
            }
            catch (ArgumentException ex)
            {
                var propertyChangeEvent =
                    new PropertyChangeEventArgs(_memberName, null, newValue);
                throw new TypeMismatchException(propertyChangeEvent, targetType, ex);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Constructor to use when an exception results from a
 /// <see cref="System.ComponentModel.PropertyChangedEventArgs"/>.
 /// </summary>
 /// <param name="ex">
 /// The <see cref="System.Exception"/> raised by the invoked property.
 /// </param>
 /// <param name="argument">
 /// The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> that
 /// resulted in an exception.
 /// </param>
 public MethodInvocationException(Exception ex, PropertyChangeEventArgs argument) :
     base("Property '" + argument.PropertyName + "' threw exception.", argument, ex)
 {
 }
 /// <summary>
 /// Constructor to use when an exception results from a
 /// <see cref="System.ComponentModel.PropertyChangedEventArgs"/>.
 /// </summary>
 /// <param name="ex">
 /// The <see cref="System.Exception"/> raised by the invoked property.
 /// </param>
 /// <param name="argument">
 /// The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> that
 /// resulted in an exception.
 /// </param>
 public MethodInvocationException(Exception ex, PropertyChangeEventArgs argument)
     : base("Property '" + argument.PropertyName + "' threw exception.", argument, ex)
 {
 }
 /// <summary>
 /// Create a new instance of the PropertyAccessException class.
 /// </summary>
 /// <param name="message">
 /// A message about the exception.
 /// </param>
 /// <param name="propertyChangeEvent">Describes the change attempted on the property.</param>
 /// <param name="rootCause">
 /// The root exception that is being wrapped.
 /// </param>
 protected PropertyAccessException(string message, PropertyChangeEventArgs propertyChangeEvent, Exception rootCause)
     : base(message, rootCause)
 {
     _propertyChangeEventArgs = propertyChangeEvent;
 }
 /// <summary>
 /// Creates a new instance of the PropertyAccessExceptionsException class.
 /// </summary>
 /// <param name="info">
 /// The <see cref="System.Runtime.Serialization.SerializationInfo"/>
 /// that holds the serialized object data about the exception being thrown.
 /// </param>
 /// <param name="context">
 /// The <see cref="System.Runtime.Serialization.StreamingContext"/>
 /// that contains contextual information about the source or destination.
 /// </param>
 protected PropertyAccessException(
     SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _propertyChangeEventArgs = info.GetValue("PropertyChangeArgs", typeof (object)) as PropertyChangeEventArgs;
 }
 /// <summary>
 /// Creates a new instance of the TypeMismatchException class describing the
 /// property, required type, and underlying exception that could not be used
 /// to set a property on the target object.
 /// </summary>
 /// <param name="propertyChangeEventArgs">
 /// The description of the property that was to be changed.
 /// </param>
 /// <param name="requiredType">The target conversion type.</param>
 /// <param name="rootCause">The underlying exception.</param>
 public TypeMismatchException(
     PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType, Exception rootCause)
     : base(BuildMessage(propertyChangeEventArgs, requiredType), propertyChangeEventArgs, rootCause)
 {
 }
 private static string BuildMessage(PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType)
 {
     var message = new StringBuilder();
     message.Append("Cannot convert property value of type [");
     if (propertyChangeEventArgs != null && propertyChangeEventArgs.NewValue != null)
     {
         message.Append(propertyChangeEventArgs.NewValue.GetType().FullName);
     }
     else
     {
         message.Append("null");
     }
     message.Append("] to required type [");
     if (requiredType != null)
     {
         message.Append(requiredType.FullName);
     }
     else
     {
         message.Append("null");
     }
     message.Append("] for property '");
     if (propertyChangeEventArgs != null && propertyChangeEventArgs.PropertyName != null)
     {
         message.Append(propertyChangeEventArgs.PropertyName);
     }
     message.Append("'.");
     return message.ToString();
 }
 /// <summary>
 /// Creates a new instance of the TypeMismatchException class describing the
 /// property, required type, and underlying exception that could not be used
 /// to set a property on the target object.
 /// </summary>
 /// <param name="propertyChangeEventArgs">
 /// The description of the property that was to be changed.
 /// </param>
 /// <param name="requiredType">The target conversion type.</param>
 /// <param name="rootCause">The underlying exception.</param>
 public TypeMismatchException(
     PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType, Exception rootCause) :
     base(BuildMessage(propertyChangeEventArgs, requiredType), propertyChangeEventArgs, rootCause)
 {
 }
 /// <summary>
 /// Create a new instance of the PropertyAccessException class.
 /// </summary>
 /// <param name="message">
 /// A message about the exception.
 /// </param>
 /// <param name="propertyChangeEvent">Describes the change attempted on the property.</param>
 /// <param name="rootCause">
 /// The root exception that is being wrapped.
 /// </param>
 protected PropertyAccessException(string message, PropertyChangeEventArgs propertyChangeEvent, Exception rootCause)
     : base(message, rootCause)
 {
     _propertyChangeEventArgs = propertyChangeEvent;
 }
 /// <summary>
 /// Creates a new instance of the PropertyAccessExceptionsException class.
 /// </summary>
 /// <param name="info">
 /// The <see cref="System.Runtime.Serialization.SerializationInfo"/>
 /// that holds the serialized object data about the exception being thrown.
 /// </param>
 /// <param name="context">
 /// The <see cref="System.Runtime.Serialization.StreamingContext"/>
 /// that contains contextual information about the source or destination.
 /// </param>
 protected PropertyAccessException(
     SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _propertyChangeEventArgs = info.GetValue("PropertyChangeArgs", typeof(object)) as PropertyChangeEventArgs;
 }