예제 #1
0
        private ValidationErrorCollection ValidateDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            Attribute[]      validationVisibilityAtrributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(ValidationOptionAttribute));
            ValidationOption validationVisibility           = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;

            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_ContextStackItemMissing, typeof(Activity).FullName));
            }

            PropertyValidationContext propertyValidationContext = new PropertyValidationContext(activity, dependencyProperty);

            manager.Context.Push(propertyValidationContext);

            try
            {
                if (dependencyProperty.DefaultMetadata.DefaultValue != null)
                {
                    if (!dependencyProperty.PropertyType.IsValueType &&
                        dependencyProperty.PropertyType != typeof(string))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_PropertyDefaultIsReference, dependencyProperty.Name), ErrorNumbers.Error_PropertyDefaultIsReference));
                    }
                    else if (!dependencyProperty.PropertyType.IsAssignableFrom(dependencyProperty.DefaultMetadata.DefaultValue.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_PropertyDefaultTypeMismatch, dependencyProperty.Name, dependencyProperty.PropertyType.FullName, dependencyProperty.DefaultMetadata.DefaultValue.GetType().FullName), ErrorNumbers.Error_PropertyDefaultTypeMismatch));
                    }
                }

                // If an event is of type Bind, GetBinding will return the Bind object.
                object propValue = null;
                if (dependencyObject.IsBindingSet(dependencyProperty))
                {
                    propValue = dependencyObject.GetBinding(dependencyProperty);
                }
                else if (!dependencyProperty.IsEvent)
                {
                    propValue = dependencyObject.GetValue(dependencyProperty);
                }

                if (propValue == null || propValue == dependencyProperty.DefaultMetadata.DefaultValue)
                {
                    if (dependencyProperty.IsEvent)
                    {
                        // Is this added through "+=" in InitializeComponent?  If so, the value should be in the instance properties hashtable
                        // If none of these, its value is stored in UserData at design time.
                        propValue = dependencyObject.GetHandler(dependencyProperty);
                        if (propValue == null)
                        {
                            propValue = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, dependencyProperty.Name);
                        }

                        if (propValue is string && !string.IsNullOrEmpty((string)propValue))
                        {
                            errors.AddRange(ValidateEvent(activity, dependencyProperty, propValue, manager));
                        }
                    }
                    else
                    {
                        // Maybe this is an instance property.
                        propValue = dependencyObject.GetValue(dependencyProperty);
                    }
                }

                // Be careful before changing this. This is the (P || C) validation validation
                // i.e. validate properties being set only if Parent is set or
                // a child exists.
                bool checkNotSet = (activity.Parent != null) || ((activity is CompositeActivity) && (((CompositeActivity)activity).EnabledActivities.Count != 0));

                if (validationVisibility == ValidationOption.Required &&
                    (propValue == null || (propValue is string && string.IsNullOrEmpty((string)propValue))) &&
                    (dependencyProperty.DefaultMetadata.IsMetaProperty) &&
                    checkNotSet)
                {
                    errors.Add(ValidationError.GetNotSetValidationError(GetFullPropertyName(manager)));
                }
                else if (propValue != null)
                {
                    if (propValue is IList)
                    {
                        PropertyValidationContext childContext = new PropertyValidationContext(propValue, null, String.Empty);
                        manager.Context.Push(childContext);

                        try
                        {
                            foreach (object child in (IList)propValue)
                            {
                                errors.AddRange(ValidationHelpers.ValidateObject(manager, child));
                            }
                        }
                        finally
                        {
                            System.Diagnostics.Debug.Assert(manager.Context.Current == childContext, "Unwinding contextStack: the item that is about to be popped is not the one we pushed.");
                            manager.Context.Pop();
                        }
                    }
                    else if (dependencyProperty.ValidatorType != null)
                    {
                        Validator validator = null;
                        try
                        {
                            validator = Activator.CreateInstance(dependencyProperty.ValidatorType) as Validator;
                            if (validator == null)
                            {
                                errors.Add(new ValidationError(SR.GetString(SR.Error_CreateValidator, dependencyProperty.ValidatorType.FullName), ErrorNumbers.Error_CreateValidator));
                            }
                            else
                            {
                                errors.AddRange(validator.Validate(manager, propValue));
                            }
                        }
                        catch
                        {
                            errors.Add(new ValidationError(SR.GetString(SR.Error_CreateValidator, dependencyProperty.ValidatorType.FullName), ErrorNumbers.Error_CreateValidator));
                        }
                    }
                    else
                    {
                        errors.AddRange(ValidationHelpers.ValidateObject(manager, propValue));
                    }
                }
            }
            finally
            {
                System.Diagnostics.Debug.Assert(manager.Context.Current == propertyValidationContext, "Unwinding contextStack: the item that is about to be popped is not the one we pushed.");
                manager.Context.Pop();
            }

            return(errors);
        }
        private ValidationErrorCollection ValidateDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            Attribute[]      attributes    = dependencyProperty.DefaultMetadata.GetAttributes(typeof(ValidationOptionAttribute));
            ValidationOption option        = (attributes.Length > 0) ? ((ValidationOptionAttribute)attributes[0]).ValidationOption : ValidationOption.Optional;
            Activity         propertyOwner = manager.Context[typeof(Activity)] as Activity;

            if (propertyOwner == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).FullName }));
            }
            PropertyValidationContext context = new PropertyValidationContext(propertyOwner, dependencyProperty);

            manager.Context.Push(context);
            try
            {
                if (dependencyProperty.DefaultMetadata.DefaultValue != null)
                {
                    if (!dependencyProperty.PropertyType.IsValueType && (dependencyProperty.PropertyType != typeof(string)))
                    {
                        errors.Add(new ValidationError(SR.GetString("Error_PropertyDefaultIsReference", new object[] { dependencyProperty.Name }), 0x1a8));
                    }
                    else if (!dependencyProperty.PropertyType.IsAssignableFrom(dependencyProperty.DefaultMetadata.DefaultValue.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString("Error_PropertyDefaultTypeMismatch", new object[] { dependencyProperty.Name, dependencyProperty.PropertyType.FullName, dependencyProperty.DefaultMetadata.DefaultValue.GetType().FullName }), 0x1a9));
                    }
                }
                object propValue = null;
                if (dependencyObject.IsBindingSet(dependencyProperty))
                {
                    propValue = dependencyObject.GetBinding(dependencyProperty);
                }
                else if (!dependencyProperty.IsEvent)
                {
                    propValue = dependencyObject.GetValue(dependencyProperty);
                }
                if ((propValue == null) || (propValue == dependencyProperty.DefaultMetadata.DefaultValue))
                {
                    if (dependencyProperty.IsEvent)
                    {
                        propValue = dependencyObject.GetHandler(dependencyProperty);
                        if (propValue == null)
                        {
                            propValue = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, dependencyProperty.Name);
                        }
                        if ((propValue is string) && !string.IsNullOrEmpty((string)propValue))
                        {
                            errors.AddRange(this.ValidateEvent(propertyOwner, dependencyProperty, propValue, manager));
                        }
                    }
                    else
                    {
                        propValue = dependencyObject.GetValue(dependencyProperty);
                    }
                }
                bool flag = (propertyOwner.Parent != null) || ((propertyOwner is CompositeActivity) && (((CompositeActivity)propertyOwner).EnabledActivities.Count != 0));
                if (((option == ValidationOption.Required) && ((propValue == null) || ((propValue is string) && string.IsNullOrEmpty((string)propValue)))) && (dependencyProperty.DefaultMetadata.IsMetaProperty && flag))
                {
                    errors.Add(ValidationError.GetNotSetValidationError(base.GetFullPropertyName(manager)));
                    return(errors);
                }
                if (propValue == null)
                {
                    return(errors);
                }
                if (propValue is IList)
                {
                    PropertyValidationContext context2 = new PropertyValidationContext(propValue, null, string.Empty);
                    manager.Context.Push(context2);
                    try
                    {
                        foreach (object obj3 in (IList)propValue)
                        {
                            errors.AddRange(ValidationHelpers.ValidateObject(manager, obj3));
                        }
                        return(errors);
                    }
                    finally
                    {
                        manager.Context.Pop();
                    }
                }
                if (dependencyProperty.ValidatorType != null)
                {
                    Validator validator = null;
                    try
                    {
                        validator = Activator.CreateInstance(dependencyProperty.ValidatorType) as Validator;
                        if (validator == null)
                        {
                            errors.Add(new ValidationError(SR.GetString("Error_CreateValidator", new object[] { dependencyProperty.ValidatorType.FullName }), 0x106));
                            return(errors);
                        }
                        errors.AddRange(validator.Validate(manager, propValue));
                        return(errors);
                    }
                    catch
                    {
                        errors.Add(new ValidationError(SR.GetString("Error_CreateValidator", new object[] { dependencyProperty.ValidatorType.FullName }), 0x106));
                        return(errors);
                    }
                }
                errors.AddRange(ValidationHelpers.ValidateObject(manager, propValue));
            }
            finally
            {
                manager.Context.Pop();
            }
            return(errors);
        }