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); }
public void LoggerTest() { _loggerOptions = BindHelpers.FillLoggerOptions(); var options = Options.Create(_loggerOptions); _loggerService = new LoggerService(options); Action action = () => _loggerService.LogMessage("hi..", MessageType.Error); AssertHelpers.DoesNotThrows <Exception>(action); }
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); }
internal static bool CreateField(ITypeDescriptorContext context, ActivityBind activityBind, bool throwOnError) { if (!string.IsNullOrEmpty(activityBind.Path)) { Type toType = PropertyDescriptorUtils.GetBaseType(context.PropertyDescriptor, context.Instance, context); Activity component = PropertyDescriptorUtils.GetComponent(context) as Activity; if ((component != null) && (toType != null)) { component = Helpers.ParseActivityForBind(component, activityBind.Name); if (component == Helpers.GetRootActivity(component)) { bool ignoreCase = CompilerHelpers.GetSupportedLanguage(context) == SupportedLanguages.VB; Type dataSourceClass = Helpers.GetDataSourceClass(component, context); if (dataSourceClass != null) { string path = activityBind.Path; int length = path.IndexOfAny(new char[] { '.', '/', '[' }); if (length != -1) { path = path.Substring(0, length); } MemberInfo info = FindMatchingMember(path, dataSourceClass, ignoreCase); if (info != null) { Type fromType = null; bool isPrivate = false; if (info is FieldInfo) { isPrivate = ((FieldInfo) info).IsPrivate; fromType = ((FieldInfo) info).FieldType; } else if (info is PropertyInfo) { MethodInfo getMethod = ((PropertyInfo) info).GetGetMethod(); MethodInfo setMethod = ((PropertyInfo) info).GetSetMethod(); isPrivate = ((getMethod != null) && getMethod.IsPrivate) || ((setMethod != null) && setMethod.IsPrivate); } else if (info is MethodInfo) { isPrivate = ((MethodInfo) info).IsPrivate; } if (length != -1) { PathWalker walker = new PathWalker(); PathMemberInfoEventArgs finalEventArgs = null; walker.MemberFound = (EventHandler<PathMemberInfoEventArgs>) Delegate.Combine(walker.MemberFound, delegate (object sender, PathMemberInfoEventArgs eventArgs) { finalEventArgs = eventArgs; }); if (!walker.TryWalkPropertyPath(dataSourceClass, activityBind.Path)) { if (throwOnError) { throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName })); } return false; } fromType = BindHelpers.GetMemberType(finalEventArgs.MemberInfo); } if (((info.DeclaringType == dataSourceClass) || !isPrivate) && ((info is FieldInfo) && TypeProvider.IsAssignable(toType, fromType))) { return true; } if (throwOnError) { throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName })); } return false; } Activity activity2 = null; if (string.Compare(component.Name, path, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) { activity2 = component; } else if ((component is CompositeActivity) && (component is CompositeActivity)) { foreach (Activity activity3 in Helpers.GetAllNestedActivities(component as CompositeActivity)) { if (string.Compare(activity3.Name, path, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) { activity2 = activity3; } } } if (activity2 != null) { if (TypeProvider.IsAssignable(toType, activity2.GetType())) { return true; } if (throwOnError) { throw new InvalidOperationException(SR.GetString("Error_MemberWithSameNameExists", new object[] { activityBind.Path, dataSourceClass.FullName })); } return false; } IMemberCreationService service = context.GetService(typeof(IMemberCreationService)) as IMemberCreationService; if (service != null) { IDesignerHost host = context.GetService(typeof(IDesignerHost)) as IDesignerHost; if (host != null) { service.CreateField(host.RootComponentClassName, activityBind.Path, toType, null, MemberAttributes.Public, null, false); return true; } if (throwOnError) { throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName })); } } else if (throwOnError) { throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IMemberCreationService).FullName })); } } } } else { if ((component == null) && throwOnError) { throw new InvalidOperationException(SR.GetString("Error_InvalidActivityIdentifier", new object[] { activityBind.Name })); } if ((toType == null) && throwOnError) { throw new InvalidOperationException(SR.GetString("Error_PropertyTypeNotDefined", new object[] { context.PropertyDescriptor.Name, typeof(ActivityBind).Name, typeof(IDynamicPropertyTypeProvider).Name })); } } } return false; }
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); }
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); }
internal static bool CreateField(ITypeDescriptorContext context, ActivityBind activityBind, bool throwOnError) { //Check if the activity is root activity and has valid design time type if (!String.IsNullOrEmpty(activityBind.Path)) { Type boundType = PropertyDescriptorUtils.GetBaseType(context.PropertyDescriptor, context.Instance, context); Activity activity = PropertyDescriptorUtils.GetComponent(context) as Activity; if (activity != null && boundType != null) { activity = Helpers.ParseActivityForBind(activity, activityBind.Name); if (activity == Helpers.GetRootActivity(activity)) { bool isVB = (CompilerHelpers.GetSupportedLanguage(context) == SupportedLanguages.VB); Type designedType = Helpers.GetDataSourceClass(activity, context); if (designedType != null) { //field path could be nested too. //need to find field only with the name up to the first dot (CimplexTypeField in the example below) //and the right type (that would be tricky if the field doesnt exist yet) //example: CimplexTypeField.myIDictionary_int_string[10].someOtherGood2 string fieldName = activityBind.Path; int indexOfDot = fieldName.IndexOfAny(new char[] { '.', '/', '[' }); if (indexOfDot != -1) { fieldName = fieldName.Substring(0, indexOfDot); //path is a nested field access } MemberInfo matchingMember = ActivityBindPropertyDescriptor.FindMatchingMember(fieldName, designedType, isVB); if (matchingMember != null) { Type memberType = null; bool isPrivate = false; if (matchingMember is FieldInfo) { isPrivate = ((FieldInfo)matchingMember).IsPrivate; memberType = ((FieldInfo)matchingMember).FieldType; } else if (matchingMember is PropertyInfo) { MethodInfo getMethod = ((PropertyInfo)matchingMember).GetGetMethod(); MethodInfo setMethod = ((PropertyInfo)matchingMember).GetSetMethod(); isPrivate = ((getMethod != null && getMethod.IsPrivate) || (setMethod != null && setMethod.IsPrivate)); } else if (matchingMember is MethodInfo) { isPrivate = ((MethodInfo)matchingMember).IsPrivate; } if (indexOfDot != -1) { //need to find the type of the member the path references (and if the path is valid at all) PathWalker pathWalker = new PathWalker(); PathMemberInfoEventArgs finalEventArgs = null; pathWalker.MemberFound += delegate(object sender, PathMemberInfoEventArgs eventArgs) { finalEventArgs = eventArgs; }; if (pathWalker.TryWalkPropertyPath(designedType, activityBind.Path)) { //successfully walked the entire path memberType = BindHelpers.GetMemberType(finalEventArgs.MemberInfo); } else { //the path is invalid if (throwOnError) { throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName)); } return(false); } } if ((matchingMember.DeclaringType == designedType || !isPrivate) && matchingMember is FieldInfo && TypeProvider.IsAssignable(boundType, memberType)) { return(true); } else { if (throwOnError) { throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName)); } return(false); } } else { // Find out if the name conflicts with an existing activity that has not be flushed in to the // code beside. An activity bind can bind to this field only if the type of the property // is the assignable from the activity type. Activity matchingActivity = null; if (string.Compare(activity.Name, fieldName, isVB ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) { matchingActivity = activity; } else if (activity is CompositeActivity) { if (activity is CompositeActivity) { foreach (Activity existingActivity in Helpers.GetAllNestedActivities(activity as CompositeActivity)) { if (string.Compare(existingActivity.Name, fieldName, isVB ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) { matchingActivity = existingActivity; } } } } if (matchingActivity != null) { if (TypeProvider.IsAssignable(boundType, matchingActivity.GetType())) { return(true); } else { if (throwOnError) { throw new InvalidOperationException(SR.GetString(SR.Error_MemberWithSameNameExists, activityBind.Path, designedType.FullName)); } return(false); } } } IMemberCreationService memberCreationService = context.GetService(typeof(IMemberCreationService)) as IMemberCreationService; if (memberCreationService == null) { if (throwOnError) { throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IMemberCreationService).FullName)); } } else { IDesignerHost designerHost = context.GetService(typeof(IDesignerHost)) as IDesignerHost; if (designerHost == null) { if (throwOnError) { throw new InvalidOperationException(SR.GetString("General_MissingService", typeof(IDesignerHost).FullName)); } } else { memberCreationService.CreateField(designerHost.RootComponentClassName, activityBind.Path, boundType, null, MemberAttributes.Public, null, false); return(true); } } } } } else { if (activity == null && throwOnError) { throw new InvalidOperationException(SR.GetString(SR.Error_InvalidActivityIdentifier, activityBind.Name)); } if (boundType == null && throwOnError) { throw new InvalidOperationException(SR.GetString(SR.Error_PropertyTypeNotDefined, context.PropertyDescriptor.Name, typeof(ActivityBind).Name, typeof(IDynamicPropertyTypeProvider).Name)); } } } return(false); }