public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { ValidationErrorCollection errors = base.Validate(manager, obj); Activity activity = obj as Activity; if (activity != null) { ICollection <string> is2 = activity.GetValue(Activity.SynchronizationHandlesProperty) as ICollection <string>; if (is2 == null) { return(errors); } foreach (string str in is2) { ValidationError item = ValidationHelpers.ValidateIdentifier("SynchronizationHandles", manager, str); if (item != null) { errors.Add(item); } } } return(errors); }
public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { if (manager == null) { throw new ArgumentNullException("manager"); } Activity activity = obj as Activity; if (activity == null) { throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(Activity).FullName), "obj"); } if (manager.Context == null) { throw new ArgumentException("manager", SR.GetString(SR.Error_MissingContextProperty)); } manager.Context.Push(activity); ValidationErrorCollection errors = new ValidationErrorCollection(); errors.AddRange(base.Validate(manager, obj)); if (activity.Parent == null) { errors.AddRange(ValidationHelpers.ValidateUniqueIdentifiers(activity)); if (activity.Enabled == false) { ValidationError error = new ValidationError(SR.GetString(SR.Error_RootIsNotEnabled), ErrorNumbers.Error_RootIsNotEnabled); error.PropertyName = "Enabled"; errors.Add(error); } } // validate ID property, only if it is not root activity Activity rootActivity = Helpers.GetRootActivity(activity); if (activity != rootActivity) { ValidationError identifierError = ValidationHelpers.ValidateNameProperty("Name", manager, activity.Name); if (identifierError != null) { errors.Add(identifierError); } } try { errors.AddRange(ValidateProperties(manager, obj)); } finally { System.Diagnostics.Debug.Assert(manager.Context.Current == activity, "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[] 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 ValidateMethod(ValidationManager manager, Activity activity, MethodBind bind, BindValidationContext validationBindContext) { ValidationErrorCollection errors = new ValidationErrorCollection(); if ((validationBindContext.Access & AccessTypes.Write) != 0) { ValidationError item = new ValidationError(SR.GetString("Error_HandlerReadOnly"), 0x133) { PropertyName = base.GetFullPropertyName(manager) }; errors.Add(item); return(errors); } if (!TypeProvider.IsAssignable(typeof(Delegate), validationBindContext.TargetType)) { ValidationError error2 = new ValidationError(SR.GetString("Error_TypeNotDelegate", new object[] { validationBindContext.TargetType.FullName }), 0x134) { PropertyName = base.GetFullPropertyName(manager) }; errors.Add(error2); return(errors); } string name = bind.Name; Activity enclosingActivity = Helpers.GetEnclosingActivity(activity); if ((name.IndexOf('.') != -1) && (enclosingActivity != null)) { enclosingActivity = Helpers.GetDataSourceActivity(activity, bind.Name, out name); } if (enclosingActivity == null) { ValidationError error3 = new ValidationError(SR.GetString("Error_NoEnclosingContext", new object[] { activity.Name }), 0x130) { PropertyName = base.GetFullPropertyName(manager) + ".Name" }; errors.Add(error3); return(errors); } string errorText = string.Empty; int errorNumber = -1; Type dataSourceClass = Helpers.GetDataSourceClass(enclosingActivity, manager); if (dataSourceClass == null) { errorText = SR.GetString("Error_TypeNotResolvedInMethodName", new object[] { base.GetFullPropertyName(manager) + ".Name" }); errorNumber = 0x135; } else { try { ValidationHelpers.ValidateIdentifier(manager, name); } catch (Exception exception) { errors.Add(new ValidationError(exception.Message, 0x119)); } MethodInfo method = validationBindContext.TargetType.GetMethod("Invoke"); if (method == null) { throw new Exception(SR.GetString("Error_DelegateNoInvoke", new object[] { validationBindContext.TargetType.FullName })); } List <Type> list = new List <Type>(); foreach (ParameterInfo info2 in method.GetParameters()) { list.Add(info2.ParameterType); } MethodInfo info3 = Helpers.GetMethodExactMatch(dataSourceClass, name, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, null, list.ToArray(), null); if (info3 == null) { if (dataSourceClass.GetMethod(name, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance) != null) { errorText = SR.GetString("Error_MethodSignatureMismatch", new object[] { base.GetFullPropertyName(manager) + ".Name" }); errorNumber = 310; } else { errorText = SR.GetString("Error_MethodNotExists", new object[] { base.GetFullPropertyName(manager) + ".Name", bind.Name }); errorNumber = 0x137; } } else if (!method.ReturnType.Equals(info3.ReturnType)) { errorText = SR.GetString("Error_MethodReturnTypeMismatch", new object[] { base.GetFullPropertyName(manager), method.ReturnType.FullName }); errorNumber = 0x139; } } if (errorText.Length > 0) { ValidationError error4 = new ValidationError(errorText, errorNumber) { PropertyName = base.GetFullPropertyName(manager) + ".Path" }; errors.Add(error4); } 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); }
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); }