コード例 #1
0
 protected string GetFullPropertyName(ValidationManager manager)
 {
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     string propertyName = string.Empty;
     for (int i = 0; manager.Context[i] != null; i++)
     {
         if (manager.Context[i] is PropertyValidationContext)
         {
             PropertyValidationContext context = manager.Context[i] as PropertyValidationContext;
             if (context.PropertyName == string.Empty)
             {
                 propertyName = string.Empty;
             }
             else if (propertyName == string.Empty)
             {
                 propertyName = context.PropertyName;
             }
             else
             {
                 propertyName = context.PropertyName + "." + propertyName;
             }
         }
     }
     return propertyName;
 }
コード例 #2
0
ファイル: Validator.cs プロジェクト: dox0/DotNet471RS3
        protected string GetFullPropertyName(ValidationManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            string fullName = string.Empty;

            // iterate the properties in the stack starting with the last one
            int iterator = 0;

            while (manager.Context[iterator] != null)
            {
                if (manager.Context[iterator] is PropertyValidationContext)
                {
                    PropertyValidationContext propertyValidationContext = manager.Context[iterator] as PropertyValidationContext;
                    if (propertyValidationContext.PropertyName == string.Empty)
                    {
                        fullName = string.Empty;  // property chain broke... dicard properties after break
                    }
                    else if (fullName == string.Empty)
                    {
                        fullName = propertyValidationContext.PropertyName;
                    }
                    else
                    {
                        fullName = propertyValidationContext.PropertyName + "." + fullName;
                    }
                }
                iterator++;
            }

            return(fullName);
        }
コード例 #3
0
 internal static Type GetBaseType(IServiceProvider serviceProvider, PropertyValidationContext validationContext)
 {
     Type propertyType = null;
     if (validationContext.Property is PropertyInfo)
     {
         return Helpers.GetBaseType(validationContext.Property as PropertyInfo, validationContext.PropertyOwner, serviceProvider);
     }
     if (validationContext.Property is DependencyProperty)
     {
         DependencyProperty property = validationContext.Property as DependencyProperty;
         if (property == null)
         {
             return propertyType;
         }
         if (propertyType == null)
         {
             IDynamicPropertyTypeProvider propertyOwner = validationContext.PropertyOwner as IDynamicPropertyTypeProvider;
             if (propertyOwner != null)
             {
                 propertyType = propertyOwner.GetPropertyType(serviceProvider, property.Name);
             }
         }
         if (propertyType == null)
         {
             propertyType = property.PropertyType;
         }
     }
     return propertyType;
 }
コード例 #4
0
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors = base.Validate(manager, obj);
            PropertyBind bind = obj as PropertyBind;

            if (bind == null)
            {
                throw new ArgumentException(SR.GetString("Error_UnexpectedArgumentType", new object[] { typeof(PropertyBind).FullName }), "obj");
            }
            PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext;

            if (validationContext == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(BindValidationContext).Name }));
            }
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).Name }));
            }
            ValidationError item = null;

            if (string.IsNullOrEmpty(bind.Name))
            {
                item = new ValidationError(SR.GetString("Error_PropertyNotSet", new object[] { "Name" }), 0x116)
                {
                    PropertyName = base.GetFullPropertyName(manager) + ".Name"
                };
            }
            else
            {
                BindValidationContext context2 = manager.Context[typeof(BindValidationContext)] as BindValidationContext;
                if (context2 == null)
                {
                    Type baseType = BindHelpers.GetBaseType(manager, validationContext);
                    if (baseType != null)
                    {
                        AccessTypes accessType = BindHelpers.GetAccessType(manager, validationContext);
                        context2 = new BindValidationContext(baseType, accessType);
                    }
                }
                if (context2 != null)
                {
                    Type targetType = context2.TargetType;
                    if (item == null)
                    {
                        errors.AddRange(this.ValidateBindProperty(manager, activity, bind, new BindValidationContext(targetType, context2.Access)));
                    }
                }
            }
            if (item != null)
            {
                errors.Add(item);
            }
            return(errors);
        }
コード例 #5
0
 internal static AccessTypes GetAccessType(IServiceProvider serviceProvider, PropertyValidationContext validationContext)
 {
     AccessTypes read = AccessTypes.Read;
     if (validationContext.Property is PropertyInfo)
     {
         return Helpers.GetAccessType(validationContext.Property as PropertyInfo, validationContext.PropertyOwner, serviceProvider);
     }
     if (validationContext.Property is DependencyProperty)
     {
         IDynamicPropertyTypeProvider propertyOwner = validationContext.PropertyOwner as IDynamicPropertyTypeProvider;
         if (propertyOwner != null)
         {
             read = propertyOwner.GetAccessType(serviceProvider, ((DependencyProperty) validationContext.Property).Name);
         }
     }
     return read;
 }
コード例 #6
0
ファイル: Validator.cs プロジェクト: dox0/DotNet471RS3
        internal protected ValidationErrorCollection ValidateProperty(PropertyInfo propertyInfo, object propertyOwner, object propertyValue, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            object[]                  validationVisibilityAtrributes = propertyInfo.GetCustomAttributes(typeof(ValidationOptionAttribute), true);
            ValidationOption          validationVisibility           = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;
            PropertyValidationContext propertyValidationContext      = new PropertyValidationContext(propertyOwner, propertyInfo, propertyInfo.Name);

            manager.Context.Push(propertyValidationContext);

            try
            {
                if (propertyValue != null)
                {
                    errors.AddRange(ValidationHelpers.ValidateObject(manager, propertyValue));
                    if (propertyValue is IList)
                    {
                        PropertyValidationContext childContext = new PropertyValidationContext(propertyValue, null, "");
                        manager.Context.Push(childContext);

                        try
                        {
                            foreach (object child in (IList)propertyValue)
                            {
                                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();
                        }
                    }
                }
            }
            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);
        }
コード例 #7
0
 protected internal ValidationErrorCollection ValidateProperty(PropertyInfo propertyInfo, object propertyOwner, object propertyValue, ValidationManager manager)
 {
     ValidationErrorCollection errors = new ValidationErrorCollection();
     object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(ValidationOptionAttribute), true);
     if (customAttributes.Length > 0)
     {
         ValidationOption validationOption = ((ValidationOptionAttribute) customAttributes[0]).ValidationOption;
     }
     PropertyValidationContext context = new PropertyValidationContext(propertyOwner, propertyInfo, propertyInfo.Name);
     manager.Context.Push(context);
     try
     {
         if (propertyValue == null)
         {
             return errors;
         }
         errors.AddRange(ValidationHelpers.ValidateObject(manager, propertyValue));
         if (!(propertyValue is IList))
         {
             return errors;
         }
         PropertyValidationContext context2 = new PropertyValidationContext(propertyValue, null, "");
         manager.Context.Push(context2);
         try
         {
             foreach (object obj2 in (IList) propertyValue)
             {
                 errors.AddRange(ValidationHelpers.ValidateObject(manager, obj2));
             }
             return errors;
         }
         finally
         {
             manager.Context.Pop();
         }
     }
     finally
     {
         manager.Context.Pop();
     }
     return errors;
 }
コード例 #8
0
        private ValidationErrorCollection ValidateActivityBind(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors = base.Validate(manager, obj);
            ActivityBind bind = obj as ActivityBind;
            PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext;

            if (validationContext == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(BindValidationContext).Name }));
            }
            if (!(manager.Context[typeof(Activity)] is Activity))
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).Name }));
            }
            ValidationError       item     = null;
            BindValidationContext context2 = manager.Context[typeof(BindValidationContext)] as BindValidationContext;

            if (context2 == null)
            {
                Type baseType = BindHelpers.GetBaseType(manager, validationContext);
                if (baseType != null)
                {
                    AccessTypes accessType = BindHelpers.GetAccessType(manager, validationContext);
                    context2 = new BindValidationContext(baseType, accessType);
                }
            }
            if (context2 != null)
            {
                Type targetType = context2.TargetType;
                if (item == null)
                {
                    errors.AddRange(this.ValidateActivity(manager, bind, new BindValidationContext(targetType, context2.Access)));
                }
            }
            if (item != null)
            {
                errors.Add(item);
            }
            return(errors);
        }
コード例 #9
0
        internal protected ValidationErrorCollection ValidateProperty(PropertyInfo propertyInfo, object propertyOwner, object propertyValue, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            object[] validationVisibilityAtrributes = propertyInfo.GetCustomAttributes(typeof(ValidationOptionAttribute), true);
            ValidationOption validationVisibility = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;
            PropertyValidationContext propertyValidationContext = new PropertyValidationContext(propertyOwner, propertyInfo, propertyInfo.Name);
            manager.Context.Push(propertyValidationContext);

            try
            {
                if (propertyValue != null)
                {
                    errors.AddRange(ValidationHelpers.ValidateObject(manager, propertyValue));
                    if (propertyValue is IList)
                    {
                        PropertyValidationContext childContext = new PropertyValidationContext(propertyValue, null, "");
                        manager.Context.Push(childContext);

                        try
                        {
                            foreach (object child in (IList)propertyValue)
                                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();
                        }
                    }
                }
            }
            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;
        }
コード例 #10
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);
        }
コード例 #11
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;
        }
コード例 #12
0
        private DialogResult ValidateExistingPropertyBind()
        {
            Activity activity = this.workflowOutline.SelectedActivity;
            PathInfo member = this.workflowOutline.SelectedMember;
            string propertyPath = this.workflowOutline.PropertyPath; //the path on the PathInfo will be incorrect if user had changed indexes

            if (activity == null || member == null)
            {
                string message = SR.GetString(SR.Error_BindDialogNoValidPropertySelected, GetSimpleTypeFullName(this.boundType));
                DesignerHelpers.ShowError(this.serviceProvider, message);
                return DialogResult.None;
            }

            Type parsedPropertyType = member.PropertyType;
            //lets get the same type through the type provider (otherwise this type may mismatch with the one obtained from the design time types)
            ITypeProvider typeProvider = this.serviceProvider.GetService(typeof(ITypeProvider)) as ITypeProvider;
            if (typeProvider != null && parsedPropertyType != null)
            {
                Type designTimeParsedType = typeProvider.GetType(parsedPropertyType.FullName, false);
                parsedPropertyType = (designTimeParsedType != null) ? designTimeParsedType : parsedPropertyType;
            }

            if (this.boundType != parsedPropertyType && !TypeProvider.IsAssignable(this.boundType, parsedPropertyType))
            {
                string message = SR.GetString(SR.Error_BindDialogWrongPropertyType, GetSimpleTypeFullName(parsedPropertyType), GetSimpleTypeFullName(this.boundType));
                DesignerHelpers.ShowError(this.serviceProvider, message);
                return DialogResult.None;
            }

            //this is the selected activity which property is being bound
            Activity bindingActivity = PropertyDescriptorUtils.GetComponent(this.context) as Activity;

            //this is the name of the property we are binding
            string propertyName = context.PropertyDescriptor.Name;
            if (bindingActivity == activity && member != null && member.Path.Equals(propertyName, StringComparison.Ordinal))
            {
                DesignerHelpers.ShowError(this.serviceProvider, SR.GetString(SR.Error_BindDialogCanNotBindToItself));
                return DialogResult.None;
            }

            if (activity != null && member != null)
            {
                //
                ActivityBind bind = new ActivityBind(activity.QualifiedName, propertyPath);

                ValidationManager manager = new ValidationManager(this.serviceProvider);
                PropertyValidationContext propertyValidationContext = new PropertyValidationContext(this.context.Instance, DependencyProperty.FromName(this.context.PropertyDescriptor.Name, this.context.Instance.GetType()));
                manager.Context.Append(this.context.Instance); //

                ValidationErrorCollection errors;
                using (WorkflowCompilationContext.CreateScope(manager))
                {
                    errors = ValidationHelpers.ValidateProperty(manager, bindingActivity, bind, propertyValidationContext);
                }
                if (errors != null && errors.Count > 0 && errors.HasErrors)
                {
                    string message = string.Empty;
                    for (int i = 0; i < errors.Count; i++)
                    {
                        ValidationError error = errors[i];
                        message += error.ErrorText + ((i == errors.Count - 1) ? string.Empty : "; ");
                    }

                    message = SR.GetString(SR.Error_BindDialogBindNotValid) + message;
                    DesignerHelpers.ShowError(this.serviceProvider, message);
                    return DialogResult.None;
                }
                else
                {
                    this.binding = bind;
                    return DialogResult.OK;
                }
            }

            return DialogResult.None;
        }
コード例 #13
0
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors = base.Validate(manager, obj);
            ActivityBind bind = obj as ActivityBind;

            if (bind == null)
            {
                throw new ArgumentException(SR.GetString("Error_UnexpectedArgumentType", new object[] { typeof(ActivityBind).FullName }), "obj");
            }
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).Name }));
            }
            PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext;

            if (validationContext == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(BindValidationContext).Name }));
            }
            ValidationError item = null;

            if (string.IsNullOrEmpty(bind.Name))
            {
                item = new ValidationError(SR.GetString("Error_IDNotSetForActivitySource"), 0x613)
                {
                    PropertyName = base.GetFullPropertyName(manager) + ".Name"
                };
                errors.Add(item);
                return(errors);
            }
            Activity refActivity = Helpers.ParseActivityForBind(activity, bind.Name);

            if (refActivity == null)
            {
                if (bind.Name.StartsWith("/"))
                {
                    item = new ValidationError(SR.GetString("Error_CannotResolveRelativeActivity", new object[] { bind.Name }), 0x128);
                }
                else
                {
                    item = new ValidationError(SR.GetString("Error_CannotResolveActivity", new object[] { bind.Name }), 0x129);
                }
                item.PropertyName = base.GetFullPropertyName(manager) + ".Name";
                errors.Add(item);
            }
            if (string.IsNullOrEmpty(bind.Path))
            {
                item = new ValidationError(SR.GetString("Error_PathNotSetForActivitySource"), 0x12b)
                {
                    PropertyName = base.GetFullPropertyName(manager) + ".Path"
                };
                errors.Add(item);
            }
            if ((refActivity != null) && (errors.Count == 0))
            {
                string path       = bind.Path;
                string str2       = string.Empty;
                int    startIndex = path.IndexOfAny(new char[] { '.', '/', '[' });
                if (startIndex != -1)
                {
                    str2 = path.Substring(startIndex);
                    str2 = str2.StartsWith(".") ? str2.Substring(1) : str2;
                    path = path.Substring(0, startIndex);
                }
                Type       baseType   = BindHelpers.GetBaseType(manager, validationContext);
                MemberInfo memberInfo = null;
                Type       srcType    = null;
                if (!string.IsNullOrEmpty(path))
                {
                    srcType = BindValidatorHelper.GetActivityType(manager, refActivity);
                    if (srcType != null)
                    {
                        memberInfo = MemberBind.GetMemberInfo(srcType, path);
                        if ((memberInfo == null) && str2.StartsWith("[", StringComparison.Ordinal))
                        {
                            string str3  = bind.Path.Substring(startIndex);
                            int    index = str3.IndexOf(']');
                            if (index != -1)
                            {
                                string str4 = str3.Substring(0, index + 1);
                                str2 = ((index + 1) < str3.Length) ? str3.Substring(index + 1) : string.Empty;
                                str2 = str2.StartsWith(".") ? str2.Substring(1) : str2;
                                str3 = str4;
                            }
                            path       = path + str3;
                            memberInfo = MemberBind.GetMemberInfo(srcType, path);
                        }
                    }
                }
                Validator validator = null;
                object    obj2      = null;
                if (memberInfo != null)
                {
                    string str5 = !string.IsNullOrEmpty(refActivity.QualifiedName) ? refActivity.QualifiedName : bind.Name;
                    if (memberInfo is FieldInfo)
                    {
                        obj2      = new FieldBind(str5 + "." + path, str2);
                        validator = new FieldBindValidator();
                    }
                    else if (memberInfo is MethodInfo)
                    {
                        if (typeof(Delegate).IsAssignableFrom(baseType))
                        {
                            obj2      = new MethodBind(str5 + "." + path);
                            validator = new MethodBindValidator();
                        }
                        else
                        {
                            item = new ValidationError(SR.GetString("Error_InvalidMemberType", new object[] { path, base.GetFullPropertyName(manager) }), 0x629)
                            {
                                PropertyName = base.GetFullPropertyName(manager)
                            };
                            errors.Add(item);
                        }
                    }
                    else if (memberInfo is PropertyInfo)
                    {
                        if (refActivity == activity)
                        {
                            obj2      = new PropertyBind(str5 + "." + path, str2);
                            validator = new PropertyBindValidator();
                        }
                        else
                        {
                            obj2      = bind;
                            validator = this;
                        }
                    }
                    else if (memberInfo is EventInfo)
                    {
                        obj2      = bind;
                        validator = this;
                    }
                }
                else if (((memberInfo == null) && (baseType != null)) && typeof(Delegate).IsAssignableFrom(baseType))
                {
                    obj2      = bind;
                    validator = this;
                }
                if ((validator != null) && (obj2 != null))
                {
                    if ((validator == this) && (obj2 is ActivityBind))
                    {
                        errors.AddRange(this.ValidateActivityBind(manager, obj2));
                        return(errors);
                    }
                    errors.AddRange(validator.Validate(manager, obj2));
                    return(errors);
                }
                if (item == null)
                {
                    item = new ValidationError(SR.GetString("Error_PathCouldNotBeResolvedToMember", new object[] { bind.Path, !string.IsNullOrEmpty(refActivity.QualifiedName) ? refActivity.QualifiedName : refActivity.GetType().Name }), 0x60d)
                    {
                        PropertyName = base.GetFullPropertyName(manager)
                    };
                    errors.Add(item);
                }
            }
            return(errors);
        }
 internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext, object extendedPropertyContext)
 {
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     if (propertyValidationContext == null)
     {
         throw new ArgumentNullException("propertyValidationContext");
     }
     ValidationErrorCollection errors = new ValidationErrorCollection();
     manager.Context.Push(activity);
     manager.Context.Push(propertyValidationContext);
     if (extendedPropertyContext != null)
     {
         manager.Context.Push(extendedPropertyContext);
     }
     try
     {
         errors.AddRange(ValidateObject(manager, obj));
     }
     finally
     {
         manager.Context.Pop();
         manager.Context.Pop();
         if (extendedPropertyContext != null)
         {
             manager.Context.Pop();
         }
     }
     return errors;
 }
コード例 #15
0
ファイル: ValidationHelpers.cs プロジェクト: zhufengGNSS/mono
 internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext)
 {
     return(ValidateProperty(manager, activity, obj, propertyValidationContext, null));
 }
コード例 #16
0
        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);
        }
コード例 #17
0
        private ValidationErrorCollection ValidateActivity(ValidationManager manager, ActivityBind bind, BindValidationContext validationContext)
        {
            ValidationError error = null;
            ValidationErrorCollection validationErrors = new ValidationErrorCollection();

            Activity activity = manager.Context[typeof(Activity)] as Activity;
            if (activity == null)
                throw new InvalidOperationException(SR.GetString(SR.Error_ContextStackItemMissing, typeof(Activity).Name));

            Activity refActivity = Helpers.ParseActivityForBind(activity, bind.Name);
            if (refActivity == null)
            {
                error = (bind.Name.StartsWith("/", StringComparison.Ordinal)) ? new ValidationError(SR.GetString(SR.Error_CannotResolveRelativeActivity, bind.Name), ErrorNumbers.Error_CannotResolveRelativeActivity) : new ValidationError(SR.GetString(SR.Error_CannotResolveActivity, bind.Name), ErrorNumbers.Error_CannotResolveActivity);
                error.PropertyName = GetFullPropertyName(manager) + ".Name";
            }
            else if (bind.Path == null || bind.Path.Length == 0)
            {
                error = new ValidationError(SR.GetString(SR.Error_PathNotSetForActivitySource), ErrorNumbers.Error_PathNotSetForActivitySource);
                error.PropertyName = GetFullPropertyName(manager) + ".Path";
            }
            else
            {
                // 

                if (!bind.Name.StartsWith("/", StringComparison.Ordinal) && !ValidationHelpers.IsActivitySourceInOrder(refActivity, activity))
                {
                    error = new ValidationError(SR.GetString(SR.Error_BindActivityReference, refActivity.QualifiedName, activity.QualifiedName), ErrorNumbers.Error_BindActivityReference, true);
                    error.PropertyName = GetFullPropertyName(manager) + ".Name";
                }

                IDesignerHost designerHost = manager.GetService(typeof(IDesignerHost)) as IDesignerHost;
                WorkflowDesignerLoader loader = manager.GetService(typeof(WorkflowDesignerLoader)) as WorkflowDesignerLoader;
                if (designerHost != null && loader != null)
                {
                    Type refActivityType = null;
                    if (designerHost.RootComponent == refActivity)
                    {
                        ITypeProvider typeProvider = manager.GetService(typeof(ITypeProvider)) as ITypeProvider;
                        if (typeProvider == null)
                            throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(ITypeProvider).FullName));

                        refActivityType = typeProvider.GetType(designerHost.RootComponentClassName);
                    }
                    else
                    {
                        refActivity.GetType();
                    }

                    if (refActivityType != null)
                    {
                        MemberInfo memberInfo = MemberBind.GetMemberInfo(refActivityType, bind.Path);
                        if (memberInfo == null || (memberInfo is PropertyInfo && !(memberInfo as PropertyInfo).CanRead))
                        {
                            error = new ValidationError(SR.GetString(SR.Error_InvalidMemberPath, refActivity.QualifiedName, bind.Path), ErrorNumbers.Error_InvalidMemberPath);
                            error.PropertyName = GetFullPropertyName(manager) + ".Path";
                        }
                        else
                        {
                            Type memberType = null;
                            if (memberInfo is FieldInfo)
                                memberType = ((FieldInfo)(memberInfo)).FieldType;
                            else if (memberInfo is PropertyInfo)
                                memberType = ((PropertyInfo)(memberInfo)).PropertyType;
                            else if (memberInfo is EventInfo)
                                memberType = ((EventInfo)(memberInfo)).EventHandlerType;

                            if (!DoesTargetTypeMatch(validationContext.TargetType, memberType, validationContext.Access))
                            {
                                if (typeof(WorkflowParameterBinding).IsAssignableFrom(memberInfo.DeclaringType))
                                {
                                    error = new ValidationError(SR.GetString(SR.Warning_ParameterBinding, bind.Path, refActivity.QualifiedName, validationContext.TargetType.FullName), ErrorNumbers.Warning_ParameterBinding, true);
                                    error.PropertyName = GetFullPropertyName(manager) + ".Path";
                                }
                                else
                                {
                                    error = new ValidationError(SR.GetString(SR.Error_TargetTypeMismatch, memberInfo.Name, memberType.FullName, validationContext.TargetType.FullName), ErrorNumbers.Error_TargetTypeMismatch);
                                    error.PropertyName = GetFullPropertyName(manager) + ".Path";
                                }
                            }
                        }
                    }
                }
                else
                {
                    MemberInfo memberInfo = MemberBind.GetMemberInfo(refActivity.GetType(), bind.Path);
                    if (memberInfo == null || (memberInfo is PropertyInfo && !(memberInfo as PropertyInfo).CanRead))
                    {
                        error = new ValidationError(SR.GetString(SR.Error_InvalidMemberPath, refActivity.QualifiedName, bind.Path), ErrorNumbers.Error_InvalidMemberPath);
                        error.PropertyName = GetFullPropertyName(manager) + ".Path";
                    }
                    else
                    {
                        DependencyProperty dependencyProperty = DependencyProperty.FromName(memberInfo.Name, memberInfo.DeclaringType);
                        object value = BindHelpers.ResolveActivityPath(refActivity, bind.Path);
                        if (value == null)
                        {
                            Type memberType = null;
                            if (memberInfo is FieldInfo)
                                memberType = ((FieldInfo)(memberInfo)).FieldType;
                            else if (memberInfo is PropertyInfo)
                                memberType = ((PropertyInfo)(memberInfo)).PropertyType;
                            else if (memberInfo is EventInfo)
                                memberType = ((EventInfo)(memberInfo)).EventHandlerType;

                            if (!TypeProvider.IsAssignable(typeof(ActivityBind), memberType) && !DoesTargetTypeMatch(validationContext.TargetType, memberType, validationContext.Access))
                            {
                                if (typeof(WorkflowParameterBinding).IsAssignableFrom(memberInfo.DeclaringType))
                                {
                                    error = new ValidationError(SR.GetString(SR.Warning_ParameterBinding, bind.Path, refActivity.QualifiedName, validationContext.TargetType.FullName), ErrorNumbers.Warning_ParameterBinding, true);
                                    error.PropertyName = GetFullPropertyName(manager) + ".Path";
                                }
                                else
                                {
                                    error = new ValidationError(SR.GetString(SR.Error_TargetTypeMismatch, memberInfo.Name, memberType.FullName, validationContext.TargetType.FullName), ErrorNumbers.Error_TargetTypeMismatch);
                                    error.PropertyName = GetFullPropertyName(manager) + ".Path";
                                }
                            }
                        }
                        // If this is the top level activity, we should not valid that the bind can be resolved because
                        // the value of bind can be set when this activity is used in another activity.
                        else if (value is ActivityBind && refActivity.Parent != null)
                        {
                            ActivityBind referencedBind = value as ActivityBind;
                            bool bindRecursionContextAdded = false;

                            // Check for recursion
                            BindRecursionContext recursionContext = manager.Context[typeof(BindRecursionContext)] as BindRecursionContext;
                            if (recursionContext == null)
                            {
                                recursionContext = new BindRecursionContext();
                                manager.Context.Push(recursionContext);
                                bindRecursionContextAdded = true;
                            }
                            if (recursionContext.Contains(activity, bind))
                            {
                                error = new ValidationError(SR.GetString(SR.Bind_ActivityDataSourceRecursionDetected), ErrorNumbers.Bind_ActivityDataSourceRecursionDetected);
                                error.PropertyName = GetFullPropertyName(manager) + ".Path";
                            }
                            else
                            {
                                recursionContext.Add(activity, bind);

                                PropertyValidationContext propertyValidationContext = null;
                                if (dependencyProperty != null)
                                    propertyValidationContext = new PropertyValidationContext(refActivity, dependencyProperty);
                                else
                                    propertyValidationContext = new PropertyValidationContext(refActivity, memberInfo as PropertyInfo, memberInfo.Name);

                                validationErrors.AddRange(ValidationHelpers.ValidateProperty(manager, refActivity, referencedBind, propertyValidationContext, validationContext));
                            }

                            if (bindRecursionContextAdded)
                                manager.Context.Pop();
                        }
                        else if (validationContext.TargetType != null && !DoesTargetTypeMatch(validationContext.TargetType, value.GetType(), validationContext.Access))
                        {
                            error = new ValidationError(SR.GetString(SR.Error_TargetTypeMismatch, memberInfo.Name, value.GetType().FullName, validationContext.TargetType.FullName), ErrorNumbers.Error_TargetTypeMismatch);
                            error.PropertyName = GetFullPropertyName(manager) + ".Path";
                        }
                    }
                }
            }

            if (error != null)
                validationErrors.Add(error);

            return validationErrors;
        }
 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;
 }
コード例 #19
0
ファイル: ValidationHelpers.cs プロジェクト: zhufengGNSS/mono
        internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext, object extendedPropertyContext)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (propertyValidationContext == null)
            {
                throw new ArgumentNullException("propertyValidationContext");
            }

            ValidationErrorCollection errors = new ValidationErrorCollection();

            manager.Context.Push(activity);
            manager.Context.Push(propertyValidationContext);
            if (extendedPropertyContext != null)
            {
                manager.Context.Push(extendedPropertyContext);
            }
            try
            {
                errors.AddRange(ValidationHelpers.ValidateObject(manager, obj));
            }
            finally
            {
                manager.Context.Pop();
                manager.Context.Pop();
                if (extendedPropertyContext != null)
                {
                    manager.Context.Pop();
                }
            }

            return(errors);
        }
コード例 #20
0
ファイル: Bind.cs プロジェクト: nlh774/DotNetReferenceSource
        internal static AccessTypes GetAccessType(IServiceProvider serviceProvider, PropertyValidationContext validationContext)
        {
            AccessTypes accessType = AccessTypes.Read;
            if (validationContext.Property is PropertyInfo)
            {
                accessType = Helpers.GetAccessType(validationContext.Property as PropertyInfo, validationContext.PropertyOwner, serviceProvider);
            }
            else if (validationContext.Property is DependencyProperty)
            {
                IDynamicPropertyTypeProvider basetypeProvider = validationContext.PropertyOwner as IDynamicPropertyTypeProvider;
                if (basetypeProvider != null)
                    accessType = basetypeProvider.GetAccessType(serviceProvider, ((DependencyProperty)validationContext.Property).Name);
            }

            return accessType;
        }
 internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext)
 {
     return ValidateProperty(manager, activity, obj, propertyValidationContext, null);
 }
コード例 #22
0
ファイル: Bind.cs プロジェクト: nlh774/DotNetReferenceSource
        internal static Type GetBaseType(IServiceProvider serviceProvider, PropertyValidationContext validationContext)
        {
            Type type = null;
            if (validationContext.Property is PropertyInfo)
            {
                type = Helpers.GetBaseType(validationContext.Property as PropertyInfo, validationContext.PropertyOwner, serviceProvider);
            }
            else if (validationContext.Property is DependencyProperty)
            {
                //
                DependencyProperty dependencyProperty = validationContext.Property as DependencyProperty;
                if (dependencyProperty != null)
                {
                    if (type == null)
                    {
                        IDynamicPropertyTypeProvider basetypeProvider = validationContext.PropertyOwner as IDynamicPropertyTypeProvider;
                        if (basetypeProvider != null)
                            type = basetypeProvider.GetPropertyType(serviceProvider, dependencyProperty.Name);
                    }

                    if (type == null)
                        type = dependencyProperty.PropertyType;
                }
            }

            return type;
        }
 private DialogResult ValidateExistingPropertyBind()
 {
     ValidationErrorCollection errors;
     Activity selectedActivity = this.workflowOutline.SelectedActivity;
     PathInfo selectedMember = this.workflowOutline.SelectedMember;
     string propertyPath = this.workflowOutline.PropertyPath;
     if ((selectedActivity == null) || (selectedMember == null))
     {
         string message = SR.GetString("Error_BindDialogNoValidPropertySelected", new object[] { this.GetSimpleTypeFullName(this.boundType) });
         DesignerHelpers.ShowError(this.serviceProvider, message);
         return DialogResult.None;
     }
     System.Type propertyType = selectedMember.PropertyType;
     ITypeProvider service = this.serviceProvider.GetService(typeof(ITypeProvider)) as ITypeProvider;
     if ((service != null) && (propertyType != null))
     {
         System.Type type = service.GetType(propertyType.FullName, false);
         propertyType = (type != null) ? type : propertyType;
     }
     if ((this.boundType != propertyType) && !TypeProvider.IsAssignable(this.boundType, propertyType))
     {
         string str3 = SR.GetString("Error_BindDialogWrongPropertyType", new object[] { this.GetSimpleTypeFullName(propertyType), this.GetSimpleTypeFullName(this.boundType) });
         DesignerHelpers.ShowError(this.serviceProvider, str3);
         return DialogResult.None;
     }
     Activity component = PropertyDescriptorUtils.GetComponent(this.context) as Activity;
     string name = this.context.PropertyDescriptor.Name;
     if (((component == selectedActivity) && (selectedMember != null)) && selectedMember.Path.Equals(name, StringComparison.Ordinal))
     {
         DesignerHelpers.ShowError(this.serviceProvider, SR.GetString("Error_BindDialogCanNotBindToItself"));
         return DialogResult.None;
     }
     if ((selectedActivity == null) || (selectedMember == null))
     {
         return DialogResult.None;
     }
     ActivityBind bind = new ActivityBind(selectedActivity.QualifiedName, propertyPath);
     ValidationManager serviceProvider = new ValidationManager(this.serviceProvider);
     PropertyValidationContext propertyValidationContext = new PropertyValidationContext(this.context.Instance, DependencyProperty.FromName(this.context.PropertyDescriptor.Name, this.context.Instance.GetType()));
     serviceProvider.Context.Append(this.context.Instance);
     using (WorkflowCompilationContext.CreateScope(serviceProvider))
     {
         errors = ValidationHelpers.ValidateProperty(serviceProvider, component, bind, propertyValidationContext);
     }
     if (((errors != null) && (errors.Count > 0)) && errors.HasErrors)
     {
         string str5 = string.Empty;
         for (int i = 0; i < errors.Count; i++)
         {
             ValidationError error = errors[i];
             str5 = str5 + error.ErrorText + ((i == (errors.Count - 1)) ? string.Empty : "; ");
         }
         str5 = SR.GetString("Error_BindDialogBindNotValid") + str5;
         DesignerHelpers.ShowError(this.serviceProvider, str5);
         return DialogResult.None;
     }
     this.binding = bind;
     return DialogResult.OK;
 }
コード例 #24
0
        private ValidationErrorCollection ValidateActivity(ValidationManager manager, ActivityBind bind, BindValidationContext validationContext)
        {
            ValidationError           item   = null;
            ValidationErrorCollection errors = new ValidationErrorCollection();
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).Name }));
            }
            Activity request = Helpers.ParseActivityForBind(activity, bind.Name);

            if (request == null)
            {
                item = bind.Name.StartsWith("/", StringComparison.Ordinal) ? new ValidationError(SR.GetString("Error_CannotResolveRelativeActivity", new object[] { bind.Name }), 0x128) : new ValidationError(SR.GetString("Error_CannotResolveActivity", new object[] { bind.Name }), 0x129);
                item.PropertyName = base.GetFullPropertyName(manager) + ".Name";
            }
            else if ((bind.Path == null) || (bind.Path.Length == 0))
            {
                item = new ValidationError(SR.GetString("Error_PathNotSetForActivitySource"), 0x12b)
                {
                    PropertyName = base.GetFullPropertyName(manager) + ".Path"
                };
            }
            else
            {
                if (!bind.Name.StartsWith("/", StringComparison.Ordinal) && !ValidationHelpers.IsActivitySourceInOrder(request, activity))
                {
                    item = new ValidationError(SR.GetString("Error_BindActivityReference", new object[] { request.QualifiedName, activity.QualifiedName }), 0x12a, true)
                    {
                        PropertyName = base.GetFullPropertyName(manager) + ".Name"
                    };
                }
                IDesignerHost          service = manager.GetService(typeof(IDesignerHost)) as IDesignerHost;
                WorkflowDesignerLoader loader  = manager.GetService(typeof(WorkflowDesignerLoader)) as WorkflowDesignerLoader;
                if ((service != null) && (loader != null))
                {
                    Type srcType = null;
                    if (service.RootComponent == request)
                    {
                        ITypeProvider provider = manager.GetService(typeof(ITypeProvider)) as ITypeProvider;
                        if (provider == null)
                        {
                            throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(ITypeProvider).FullName }));
                        }
                        srcType = provider.GetType(service.RootComponentClassName);
                    }
                    else
                    {
                        request.GetType();
                    }
                    if (srcType != null)
                    {
                        MemberInfo memberInfo = MemberBind.GetMemberInfo(srcType, bind.Path);
                        if ((memberInfo == null) || ((memberInfo is PropertyInfo) && !(memberInfo as PropertyInfo).CanRead))
                        {
                            item = new ValidationError(SR.GetString("Error_InvalidMemberPath", new object[] { request.QualifiedName, bind.Path }), 300)
                            {
                                PropertyName = base.GetFullPropertyName(manager) + ".Path"
                            };
                        }
                        else
                        {
                            Type memberType = null;
                            if (memberInfo is FieldInfo)
                            {
                                memberType = ((FieldInfo)memberInfo).FieldType;
                            }
                            else if (memberInfo is PropertyInfo)
                            {
                                memberType = ((PropertyInfo)memberInfo).PropertyType;
                            }
                            else if (memberInfo is EventInfo)
                            {
                                memberType = ((EventInfo)memberInfo).EventHandlerType;
                            }
                            if (!DoesTargetTypeMatch(validationContext.TargetType, memberType, validationContext.Access))
                            {
                                if (typeof(WorkflowParameterBinding).IsAssignableFrom(memberInfo.DeclaringType))
                                {
                                    item = new ValidationError(SR.GetString("Warning_ParameterBinding", new object[] { bind.Path, request.QualifiedName, validationContext.TargetType.FullName }), 0x624, true)
                                    {
                                        PropertyName = base.GetFullPropertyName(manager) + ".Path"
                                    };
                                }
                                else
                                {
                                    item = new ValidationError(SR.GetString("Error_TargetTypeMismatch", new object[] { memberInfo.Name, memberType.FullName, validationContext.TargetType.FullName }), 0x12d)
                                    {
                                        PropertyName = base.GetFullPropertyName(manager) + ".Path"
                                    };
                                }
                            }
                        }
                    }
                }
                else
                {
                    MemberInfo info2 = MemberBind.GetMemberInfo(request.GetType(), bind.Path);
                    if ((info2 == null) || ((info2 is PropertyInfo) && !(info2 as PropertyInfo).CanRead))
                    {
                        item = new ValidationError(SR.GetString("Error_InvalidMemberPath", new object[] { request.QualifiedName, bind.Path }), 300)
                        {
                            PropertyName = base.GetFullPropertyName(manager) + ".Path"
                        };
                    }
                    else
                    {
                        DependencyProperty dependencyProperty = DependencyProperty.FromName(info2.Name, info2.DeclaringType);
                        object             obj2 = BindHelpers.ResolveActivityPath(request, bind.Path);
                        if (obj2 == null)
                        {
                            Type fromType = null;
                            if (info2 is FieldInfo)
                            {
                                fromType = ((FieldInfo)info2).FieldType;
                            }
                            else if (info2 is PropertyInfo)
                            {
                                fromType = ((PropertyInfo)info2).PropertyType;
                            }
                            else if (info2 is EventInfo)
                            {
                                fromType = ((EventInfo)info2).EventHandlerType;
                            }
                            if (!TypeProvider.IsAssignable(typeof(ActivityBind), fromType) && !DoesTargetTypeMatch(validationContext.TargetType, fromType, validationContext.Access))
                            {
                                if (typeof(WorkflowParameterBinding).IsAssignableFrom(info2.DeclaringType))
                                {
                                    item = new ValidationError(SR.GetString("Warning_ParameterBinding", new object[] { bind.Path, request.QualifiedName, validationContext.TargetType.FullName }), 0x624, true)
                                    {
                                        PropertyName = base.GetFullPropertyName(manager) + ".Path"
                                    };
                                }
                                else
                                {
                                    item = new ValidationError(SR.GetString("Error_TargetTypeMismatch", new object[] { info2.Name, fromType.FullName, validationContext.TargetType.FullName }), 0x12d)
                                    {
                                        PropertyName = base.GetFullPropertyName(manager) + ".Path"
                                    };
                                }
                            }
                        }
                        else if ((obj2 is ActivityBind) && (request.Parent != null))
                        {
                            ActivityBind         bind2   = obj2 as ActivityBind;
                            bool                 flag    = false;
                            BindRecursionContext context = manager.Context[typeof(BindRecursionContext)] as BindRecursionContext;
                            if (context == null)
                            {
                                context = new BindRecursionContext();
                                manager.Context.Push(context);
                                flag = true;
                            }
                            if (context.Contains(activity, bind))
                            {
                                item = new ValidationError(SR.GetString("Bind_ActivityDataSourceRecursionDetected"), 0x12f)
                                {
                                    PropertyName = base.GetFullPropertyName(manager) + ".Path"
                                };
                            }
                            else
                            {
                                context.Add(activity, bind);
                                PropertyValidationContext propertyValidationContext = null;
                                if (dependencyProperty != null)
                                {
                                    propertyValidationContext = new PropertyValidationContext(request, dependencyProperty);
                                }
                                else
                                {
                                    propertyValidationContext = new PropertyValidationContext(request, info2 as PropertyInfo, info2.Name);
                                }
                                errors.AddRange(ValidationHelpers.ValidateProperty(manager, request, bind2, propertyValidationContext, validationContext));
                            }
                            if (flag)
                            {
                                manager.Context.Pop();
                            }
                        }
                        else if ((validationContext.TargetType != null) && !DoesTargetTypeMatch(validationContext.TargetType, obj2.GetType(), validationContext.Access))
                        {
                            item = new ValidationError(SR.GetString("Error_TargetTypeMismatch", new object[] { info2.Name, obj2.GetType().FullName, validationContext.TargetType.FullName }), 0x12d)
                            {
                                PropertyName = base.GetFullPropertyName(manager) + ".Path"
                            };
                        }
                    }
                }
            }
            if (item != null)
            {
                errors.Add(item);
            }
            return(errors);
        }