Пример #1
0
        private static string BuildMessage(PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType)
        {
            StringBuilder 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>
 /// 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>
 protected PropertyAccessException(string message, PropertyChangeEventArgs propertyChangeEvent) : base(message)
 {
     _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;
 }
Пример #4
0
 /// <summary>
 /// Creates a new instance of the TypeMismatchException class describing the
 /// property and required type that could not 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>
 public TypeMismatchException(
     PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType) :
         base(BuildMessage(propertyChangeEventArgs, requiredType), propertyChangeEventArgs)
 {
 }
Пример #5
0
 private static string BuildMessage(PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType)
 {
     StringBuilder 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>
 /// 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>
 protected PropertyAccessException(string message, PropertyChangeEventArgs propertyChangeEvent) : base(message)
 {
     _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>
 /// 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)
 {
 }
Пример #10
0
        /// <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)
        {
            bool isWriteable = accessor.IsWriteable;
            Type 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 '" + this.memberName + "'.");
                    }
                }
                else if (targetType.IsPrimitive && (newValue == null || String.Empty.Equals(newValue)))
                {
                    throw new ArgumentException("Invalid value [" + newValue + "] for property or field '" +
                                                this.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 || newValue is ISet))
                {
                    if (!AddToCollections(context, evalContext, newValue))
                    {
                        object tmpValue =
                            TypeConversionUtils.ConvertValueIfNecessary(targetType, newValue, this.memberName);
                        SetPropertyOrFieldValueInternal(context, tmpValue);
                    }
                }
                else
                {
                    object tmpValue = TypeConversionUtils.ConvertValueIfNecessary(targetType, newValue, this.memberName);
                    SetPropertyOrFieldValueInternal(context, tmpValue);
                }
            }
            catch (TargetInvocationException ex)
            {
                PropertyChangeEventArgs propertyChangeEvent =
                    new PropertyChangeEventArgs(this.memberName, null, newValue);
                if (ex.GetBaseException() is InvalidCastException)
                {
                    throw new TypeMismatchException(propertyChangeEvent, targetType, ex.GetBaseException());
                }
                else
                {
                    throw new MethodInvocationException(ex.GetBaseException(), propertyChangeEvent);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new FatalReflectionException("Illegal attempt to set property '" + this.memberName + "'", ex);
            }
            catch (NotWritablePropertyException)
            {
                throw;
            }
            catch (NotReadablePropertyException)
            {
                throw;
            }
            catch (ArgumentException ex)
            {
                PropertyChangeEventArgs propertyChangeEvent =
                    new PropertyChangeEventArgs(this.memberName, null, newValue);
                throw new TypeMismatchException(propertyChangeEvent, targetType, ex);
            }
        }
Пример #11
0
 /// <summary>
 /// Creates a new instance of the TypeMismatchException class describing the
 /// property and required type that could not 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>
 public TypeMismatchException(
     PropertyChangeEventArgs propertyChangeEventArgs, Type requiredType) :
     base(BuildMessage(propertyChangeEventArgs, requiredType), propertyChangeEventArgs)
 {
 }