private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack <MemberInfo> methodStack) { ValidationError error; if (string.IsNullOrEmpty(this.methodInvoked)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, Messages.NullValue }), 0x56b, true); error.UserData["ErrorObject"] = this; validation.AddError(error); return(false); } bool flag = true; MemberInfo[] infoArray = contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if ((infoArray == null) || (infoArray.Length == 0)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, this.methodInvoked }), 0x56b, true); error.UserData["ErrorObject"] = this; validation.AddError(error); return(false); } for (int i = 0; i < infoArray.Length; i++) { MemberInfo item = infoArray[i]; if (!methodStack.Contains(item)) { methodStack.Push(item); object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length != 0)) { foreach (RuleAttribute attribute in customAttributes) { RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute; if (attribute2 != null) { if (attribute2.Target == RuleAttributeTarget.Parameter) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, new object[] { item.Name }), 0x1a5, true); error.UserData["ErrorObject"] = this; validation.AddError(error); flag = false; } else { attribute2.Validate(validation, item, contextType, null); } } else { ((RuleInvokeAttribute)attribute).ValidateInvokeAttribute(validation, item, contextType, methodStack); } } } methodStack.Pop(); } } return(flag); }
private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack<MemberInfo> methodStack) { ValidationError error; if (string.IsNullOrEmpty(this.methodInvoked)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, Messages.NullValue }), 0x56b, true); error.UserData["ErrorObject"] = this; validation.AddError(error); return false; } bool flag = true; MemberInfo[] infoArray = contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if ((infoArray == null) || (infoArray.Length == 0)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, this.methodInvoked }), 0x56b, true); error.UserData["ErrorObject"] = this; validation.AddError(error); return false; } for (int i = 0; i < infoArray.Length; i++) { MemberInfo item = infoArray[i]; if (!methodStack.Contains(item)) { methodStack.Push(item); object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length != 0)) { foreach (RuleAttribute attribute in customAttributes) { RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute; if (attribute2 != null) { if (attribute2.Target == RuleAttributeTarget.Parameter) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, new object[] { item.Name }), 0x1a5, true); error.UserData["ErrorObject"] = this; validation.AddError(error); flag = false; } else { attribute2.Validate(validation, item, contextType, null); } } else { ((RuleInvokeAttribute) attribute).ValidateInvokeAttribute(validation, item, contextType, methodStack); } } } methodStack.Pop(); } } return flag; }
public bool Validate(RuleValidation validation) { if (validation == null) { throw new ArgumentNullException("validation"); } Dictionary <string, object> dictionary = new Dictionary <string, object>(); foreach (Rule rule in this.rules) { if (!string.IsNullOrEmpty(rule.Name)) { if (dictionary.ContainsKey(rule.Name)) { ValidationError error = new ValidationError(Messages.Error_DuplicateRuleName, 0x53f); error.UserData["ErrorObject"] = rule; validation.AddError(error); } else { dictionary.Add(rule.Name, null); } } rule.Validate(validation); } if ((validation.Errors != null) && (validation.Errors.Count != 0)) { return(false); } return(true); }
public override bool Validate(RuleValidation validation) { if (validation == null) { throw new ArgumentNullException("validation"); } bool valid = true; if (_expression == null) { valid = false; string message = string.Format(CultureInfo.CurrentCulture, Messages.ConditionExpressionNull, typeof(CodePrimitiveExpression).ToString()); ValidationError error = new ValidationError(message, ErrorNumbers.Error_EmptyExpression); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); } else { valid = validation.ValidateConditionExpression(_expression); } return(valid); }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } if (this.codeDomStatement == null) { ValidationError error = new ValidationError(Messages.NullStatement, 0x53d); error.UserData["ErrorObject"] = this; validator.AddError(error); return(false); } return(CodeDomStatementWalker.Validate(validator, this.codeDomStatement)); }
public override bool Validate(RuleValidation validation) { if (validation == null) { throw new ArgumentNullException("validation"); } bool flag = true; if (this._expression == null) { flag = false; ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ConditionExpressionNull, new object[] { typeof(CodePrimitiveExpression).ToString() }), 400); error.UserData["ErrorObject"] = this; validation.AddError(error); return(flag); } return(validation.ValidateConditionExpression(this._expression)); }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } if (codeDomStatement == null) { ValidationError error = new ValidationError(Messages.NullStatement, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); return(false); } else { return(CodeDomStatementWalker.Validate(validator, codeDomStatement)); } }
public bool Validate(RuleValidation validation) { if (validation == null) { throw new ArgumentNullException("validation"); } // Validate each rule. Dictionary <string, object> ruleNames = new Dictionary <string, object>(); foreach (Rule r in rules) { if (!string.IsNullOrEmpty(r.Name)) // invalid names caught when validating the rule { if (ruleNames.ContainsKey(r.Name)) { // Duplicate rule name found. ValidationError error = new ValidationError(Messages.Error_DuplicateRuleName, ErrorNumbers.Error_DuplicateConditions); error.UserData[RuleUserDataKeys.ErrorObject] = r; validation.AddError(error); } else { ruleNames.Add(r.Name, null); } } r.Validate(validation); } if (validation.Errors == null || validation.Errors.Count == 0) { return(true); } return(false); }
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters) { ValidationError error = null; string message = null; if (string.IsNullOrEmpty(attributePath)) { // It is allowed to pass null or the empty string to [RuleRead] or [RuleWrite]. This // is how you indicate that a method or property has no dependencies or side effects. return(true); } bool valid = true; string[] parts = attributePath.Split('/'); // Check the first part. string firstPart = parts[0]; int startOfRelativePortion = 0; if (attributeTarget == RuleAttributeTarget.This) { // When target is "This", the path is allowed to start with the token "this". It is // then skipped for the rest of the validation, and the contextType remains what it // was when passed in. if (firstPart == "this") { ++startOfRelativePortion; } } else { // When target is "Parameter", the path must start with the name of a parameter. bool found = false; for (int p = 0; p < parameters.Length; ++p) { ParameterInfo param = parameters[p]; if (param.Name == firstPart) { found = true; // The context type is the parameter type. contextType = param.ParameterType; break; } } if (!found) { message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, firstPart, member.Name); error = new ValidationError(message, ErrorNumbers.Error_InvalidRuleAttributeParameter); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); return(false); } ++startOfRelativePortion; } int numParts = parts.Length; // Check the last part. The last part is allowed to be empty, or "*". string lastPart = parts[numParts - 1]; if (string.IsNullOrEmpty(lastPart) || lastPart == "*") { numParts -= 1; } // Check the rest of the parts. Type currentType = contextType; for (int i = startOfRelativePortion; i < numParts; ++i) { // Can't have embedded "*" wildcards. if (parts[i] == "*") { // The "*" occurred in the middle of the path, which is a no-no. error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; break; } // Skip array types. while (currentType.IsArray) { currentType = currentType.GetElementType(); } // Make sure the member exists in the current type. BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (validation.AllowInternalMembers(currentType)) { bindingFlags |= BindingFlags.NonPublic; } FieldInfo field = currentType.GetField(parts[i], bindingFlags); if (field != null) { currentType = field.FieldType; } else { PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags); if (property != null) { currentType = property.PropertyType; } else { message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]); error = new ValidationError(message, ErrorNumbers.Error_UnknownFieldOrProperty); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; break; } } } return(valid); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { RuleExpressionInfo info; bool flag; CodeDirectionExpression expression2 = (CodeDirectionExpression)expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeDirectionExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = expression2; validation.Errors.Add(item); return(null); } if (expression2.Expression == null) { ValidationError error2 = new ValidationError(Messages.NullDirectionTarget, 0x53d); error2.UserData["ErrorObject"] = expression2; validation.Errors.Add(error2); return(null); } if (expression2.Expression is CodeTypeReferenceExpression) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = expression2.Expression; validation.AddError(error); return(null); } if (expression2.Direction == FieldDirection.Ref) { flag = true; if (RuleExpressionWalker.Validate(validation, expression2.Expression, false) == null) { return(null); } info = RuleExpressionWalker.Validate(validation, expression2.Expression, true); } else if (expression2.Direction == FieldDirection.Out) { flag = true; info = RuleExpressionWalker.Validate(validation, expression2.Expression, true); } else { flag = false; info = RuleExpressionWalker.Validate(validation, expression2.Expression, false); } if (info == null) { return(null); } Type expressionType = info.ExpressionType; if (expressionType == null) { return(null); } if (((expressionType != typeof(NullLiteral)) && flag) && !expressionType.IsByRef) { expressionType = expressionType.MakeByRefType(); } return(new RuleExpressionInfo(expressionType)); }
public override bool Validate(RuleValidation validation) { if (validation == null) throw new ArgumentNullException("validation"); bool valid = true; if (_expression == null) { valid = false; string message = string.Format(CultureInfo.CurrentCulture, Messages.ConditionExpressionNull, typeof(CodePrimitiveExpression).ToString()); ValidationError error = new ValidationError(message, ErrorNumbers.Error_EmptyExpression); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); } else { valid = validation.ValidateConditionExpression(_expression); } return valid; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError item = null; RulePropertyExpressionInfo info = null; bool nonPublic = false; Type expressionType = null; CodeIndexerExpression newParent = (CodeIndexerExpression) expression; CodeExpression targetObject = newParent.TargetObject; if (targetObject == null) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if (targetObject is CodeTypeReferenceExpression) { item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if ((newParent.Indices == null) || (newParent.Indices.Count == 0)) { item = new ValidationError(Messages.MissingIndexExpressions, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } try { if (!validation.PushParentExpression(newParent)) { return null; } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); if (info2 == null) { return null; } expressionType = info2.ExpressionType; if (expressionType == null) { return null; } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } List<CodeExpression> argumentExprs = new List<CodeExpression>(); bool flag2 = false; for (int i = 0; i < newParent.Indices.Count; i++) { CodeExpression expression4 = newParent.Indices[i]; if (expression4 == null) { item = new ValidationError(Messages.NullIndexExpression, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag2 = true; } else { CodeDirectionExpression expression5 = expression4 as CodeDirectionExpression; if ((expression5 != null) && (expression5.Direction != FieldDirection.In)) { item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag2 = true; } if (expression4 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression4; validation.AddError(item); flag2 = true; } if (RuleExpressionWalker.Validate(validation, expression4, false) == null) { flag2 = true; } else { argumentExprs.Add(expression4); } } } if (expressionType == null) { return null; } if (flag2) { return null; } BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance; if (validation.AllowInternalMembers(expressionType)) { bindingFlags |= BindingFlags.NonPublic; nonPublic = true; } info = validation.ResolveIndexerProperty(expressionType, bindingFlags, argumentExprs, out item); if (info == null) { item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } } finally { validation.PopParentExpression(); } PropertyInfo propertyInfo = info.PropertyInfo; MethodInfo accessorMethod = isWritten ? propertyInfo.GetSetMethod(nonPublic) : propertyInfo.GetGetMethod(nonPublic); if (accessorMethod == null) { string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet; item = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { propertyInfo.Name, RuleDecompiler.DecompileType(expressionType) }), 0x54a); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if (!validation.ValidateMemberAccess(targetObject, expressionType, accessorMethod, propertyInfo.Name, newParent)) { return null; } object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { Stack<MemberInfo> stack = new Stack<MemberInfo>(); stack.Push(propertyInfo); bool flag3 = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, propertyInfo, expressionType, propertyInfo.GetIndexParameters())) { flag3 = false; } } stack.Pop(); if (!flag3) { return null; } } return info; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { string message; ValidationError error = null; Type targetType = null; CodeArrayIndexerExpression arrayIndexerExpr = (CodeArrayIndexerExpression)expression; CodeExpression targetObject = arrayIndexerExpr.TargetObject; if (targetObject == null) { error = new ValidationError(Messages.NullIndexerTarget, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } if (targetObject is CodeTypeReferenceExpression) { error = new ValidationError(Messages.IndexersCannotBeStatic, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } if (arrayIndexerExpr.Indices == null || arrayIndexerExpr.Indices.Count == 0) { error = new ValidationError(Messages.MissingIndexExpressions, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } try { // Early exit from this if a cycle is detected. if (!validation.PushParentExpression(arrayIndexerExpr)) return null; RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, arrayIndexerExpr.TargetObject, false); if (targetExprInfo == null) // error occurred, so simply return return null; targetType = targetExprInfo.ExpressionType; if (targetType == null) return null; // if an error occurred (targetType == null), continue on to validate the arguments if (targetType == typeof(NullLiteral)) { error = new ValidationError(Messages.NullIndexerTarget, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } // The target type better be an array. if (!targetType.IsArray) { message = string.Format(CultureInfo.CurrentCulture, Messages.CannotIndexType, RuleDecompiler.DecompileType(targetType)); error = new ValidationError(message, ErrorNumbers.Error_CannotIndexType); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } int rank = targetType.GetArrayRank(); if (arrayIndexerExpr.Indices.Count != rank) { message = string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadRank, rank); error = new ValidationError(message, ErrorNumbers.Error_ArrayIndexBadRank); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); return null; } bool hasInvalidArgument = false; for (int i = 0; i < arrayIndexerExpr.Indices.Count; ++i) { CodeExpression argExpr = arrayIndexerExpr.Indices[i]; if (argExpr == null) { error = new ValidationError(Messages.NullIndexExpression, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr; validation.Errors.Add(error); hasInvalidArgument = true; } else { CodeDirectionExpression argDirection = argExpr as CodeDirectionExpression; if (argDirection != null) { // No "ref" or "out" arguments are allowed on indexer arguments. error = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, ErrorNumbers.Error_IndexerArgCannotBeRefOrOut); error.UserData[RuleUserDataKeys.ErrorObject] = argExpr; validation.Errors.Add(error); hasInvalidArgument = true; } if (argExpr is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, argExpr.GetType().FullName); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = argExpr; validation.AddError(error); hasInvalidArgument = true; } // Validate the argument. RuleExpressionInfo argExprInfo = RuleExpressionWalker.Validate(validation, argExpr, false); if (argExprInfo != null) { Type argType = argExprInfo.ExpressionType; TypeCode argTypeCode = Type.GetTypeCode(argType); // Any type that is, or can be converted to: int or long. switch (argTypeCode) { case TypeCode.Byte: case TypeCode.Char: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.UInt16: break; default: message = string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadType, RuleDecompiler.DecompileType(argType)); error = new ValidationError(message, ErrorNumbers.Error_ArrayIndexBadType); error.UserData[RuleUserDataKeys.ErrorObject] = argExpr; validation.Errors.Add(error); hasInvalidArgument = true; break; } } else { hasInvalidArgument = true; } } } // Stop further validation if there was a problem with any of the arguments. if (hasInvalidArgument) return null; } finally { validation.PopParentExpression(); } // The result type is this array's element type. return new RuleExpressionInfo(targetType.GetElementType()); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { RuleExpressionInfo info; bool flag; CodeDirectionExpression expression2 = (CodeDirectionExpression) expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeDirectionExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = expression2; validation.Errors.Add(item); return null; } if (expression2.Expression == null) { ValidationError error2 = new ValidationError(Messages.NullDirectionTarget, 0x53d); error2.UserData["ErrorObject"] = expression2; validation.Errors.Add(error2); return null; } if (expression2.Expression is CodeTypeReferenceExpression) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = expression2.Expression; validation.AddError(error); return null; } if (expression2.Direction == FieldDirection.Ref) { flag = true; if (RuleExpressionWalker.Validate(validation, expression2.Expression, false) == null) { return null; } info = RuleExpressionWalker.Validate(validation, expression2.Expression, true); } else if (expression2.Direction == FieldDirection.Out) { flag = true; info = RuleExpressionWalker.Validate(validation, expression2.Expression, true); } else { flag = false; info = RuleExpressionWalker.Validate(validation, expression2.Expression, false); } if (info == null) { return null; } Type expressionType = info.ExpressionType; if (expressionType == null) { return null; } if (((expressionType != typeof(NullLiteral)) && flag) && !expressionType.IsByRef) { expressionType = expressionType.MakeByRefType(); } return new RuleExpressionInfo(expressionType); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { string message; CodeCastExpression castExpr = (CodeCastExpression)expression; if (isWritten) { message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeCastExpression).ToString()); ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } if (castExpr.Expression == null) { ValidationError error = new ValidationError(Messages.NullCastExpr, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } if (castExpr.Expression is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, castExpr.Expression.GetType().FullName); ValidationError error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr.Expression; validation.AddError(error); return null; } if (castExpr.TargetType == null) { ValidationError error = new ValidationError(Messages.NullCastType, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } // Figure out the operand type. RuleExpressionInfo operandInfo = RuleExpressionWalker.Validate(validation, castExpr.Expression, false); if (operandInfo == null) return null; Type fromType = operandInfo.ExpressionType; Type toType = validation.ResolveType(castExpr.TargetType); if (toType == null) return null; if (fromType == typeof(NullLiteral)) { // Casting from null value. if (ConditionHelper.IsNonNullableValueType(toType)) { message = string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, RuleDecompiler.DecompileType(toType)); ValidationError error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } } else { // Unwrap nullables to make life easy. Type fromType2 = fromType; if (ConditionHelper.IsNullableValueType(fromType2)) fromType2 = fromType2.GetGenericArguments()[0]; Type toType2 = toType; if (ConditionHelper.IsNullableValueType(toType2)) toType2 = toType2.GetGenericArguments()[0]; bool canConvert = false; if (fromType2.IsValueType && toType2.IsValueType) { // Convert.ChangeType doesn't handle enum <--> numeric // and float/double/decimal <--> char, which are allowed if (fromType2.IsEnum) { canConvert = (toType2.IsEnum) || IsNumeric(toType2); } else if (toType2.IsEnum) { // don't need to check fromType for enum since it's handled above canConvert = IsNumeric(fromType2); } else if (fromType2 == typeof(char)) { canConvert = IsNumeric(toType2); } else if (toType2 == typeof(char)) { canConvert = IsNumeric(fromType2); } else if (fromType2.IsPrimitive && toType2.IsPrimitive) { try { // note: this also allows bool <--> numeric conversions object fromValueDefault = Activator.CreateInstance(fromType2); Convert.ChangeType(fromValueDefault, toType2, CultureInfo.CurrentCulture); canConvert = true; } catch (Exception) { canConvert = false; } } } if (!canConvert) { // We can cast up or down an inheritence hierarchy, // as well as support explicit and implicit overrides ValidationError error; canConvert = RuleValidation.ExplicitConversionSpecified(fromType, toType, out error); if (error != null) { error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } } if (!canConvert) { message = string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, RuleDecompiler.DecompileType(fromType), RuleDecompiler.DecompileType(toType)); ValidationError error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = castExpr; validation.Errors.Add(error); return null; } } return new RuleExpressionInfo(toType); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { string message; ValidationError error = null; RulePropertyExpressionInfo propExprInfo = null; bool includeNonPublic = false; Type targetType = null; CodeIndexerExpression indexerExpr = (CodeIndexerExpression)expression; CodeExpression targetObject = indexerExpr.TargetObject; if (targetObject == null) { error = new ValidationError(Messages.NullIndexerTarget, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); return null; } if (targetObject is CodeTypeReferenceExpression) { error = new ValidationError(Messages.IndexersCannotBeStatic, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); return null; } if (indexerExpr.Indices == null || indexerExpr.Indices.Count == 0) { error = new ValidationError(Messages.MissingIndexExpressions, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); return null; } try { // Early exit from this if a cycle is detected. if (!validation.PushParentExpression(indexerExpr)) return null; RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, indexerExpr.TargetObject, false); if (targetExprInfo == null) // error occurred, so simply return return null; targetType = targetExprInfo.ExpressionType; if (targetType == null) return null; // if an error occurred (targetType == null), continue on to validate the arguments if (targetType == typeof(NullLiteral)) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget); error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); targetType = null; // force exit after validating the arguments } List<CodeExpression> argExprs = new List<CodeExpression>(); bool hasInvalidArgument = false; for (int i = 0; i < indexerExpr.Indices.Count; ++i) { CodeExpression argExpr = indexerExpr.Indices[i]; if (argExpr == null) { error = new ValidationError(Messages.NullIndexExpression, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); hasInvalidArgument = true; } else { CodeDirectionExpression argDirection = argExpr as CodeDirectionExpression; if (argDirection != null && argDirection.Direction != FieldDirection.In) { // No "ref" or "out" arguments are allowed on indexer arguments. error = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, ErrorNumbers.Error_IndexerArgCannotBeRefOrOut); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); hasInvalidArgument = true; } if (argExpr is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, argExpr.GetType().FullName); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = argExpr; validation.AddError(error); hasInvalidArgument = true; } // Validate the argument. RuleExpressionInfo argExprInfo = RuleExpressionWalker.Validate(validation, argExpr, false); if (argExprInfo == null) hasInvalidArgument = true; else argExprs.Add(argExpr); } } // Stop further validation if there was a problem with the target expression. if (targetType == null) return null; // Stop further validation if there was a problem with any of the arguments. if (hasInvalidArgument) return null; BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance; if (validation.AllowInternalMembers(targetType)) { bindingFlags |= BindingFlags.NonPublic; includeNonPublic = true; } // Everything okay so far, try to resolve the method. propExprInfo = validation.ResolveIndexerProperty(targetType, bindingFlags, argExprs, out error); if (propExprInfo == null) { error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); return null; } } finally { validation.PopParentExpression(); } PropertyInfo pi = propExprInfo.PropertyInfo; MethodInfo accessorMethod = isWritten ? pi.GetSetMethod(includeNonPublic) : pi.GetGetMethod(includeNonPublic); if (accessorMethod == null) { string baseMessage = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet; message = string.Format(CultureInfo.CurrentCulture, baseMessage, pi.Name, RuleDecompiler.DecompileType(targetType)); error = new ValidationError(message, ErrorNumbers.Error_CannotResolveMember); error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr; validation.Errors.Add(error); return null; } if (!validation.ValidateMemberAccess(targetObject, targetType, accessorMethod, pi.Name, indexerExpr)) return null; // Validate any RuleAttributes, if present. object[] attrs = pi.GetCustomAttributes(typeof(RuleAttribute), true); if (attrs != null && attrs.Length > 0) { Stack<MemberInfo> methodStack = new Stack<MemberInfo>(); methodStack.Push(pi); bool allAttributesValid = true; foreach (RuleAttribute ruleAttr in attrs) { if (!ruleAttr.Validate(validation, pi, targetType, pi.GetIndexParameters())) allAttributesValid = false; } methodStack.Pop(); if (!allAttributesValid) return null; } return propExprInfo; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { string message; ValidationError error; CodeBinaryOperatorExpression binaryExpr = (CodeBinaryOperatorExpression)expression; // Early exit from this if a cycle is detected. if (!validation.PushParentExpression(binaryExpr)) return null; if (isWritten) { message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeBinaryOperatorExpression).ToString()); error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } RuleExpressionInfo lhsExprInfo = null; RuleExpressionInfo rhsExprInfo = null; if (binaryExpr.Left == null) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, binaryExpr.Operator.ToString()); error = new ValidationError(message, ErrorNumbers.Error_LeftOperandMissing); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } else { if (binaryExpr.Left is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, binaryExpr.Left.GetType().FullName); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr.Left; validation.AddError(error); return null; } lhsExprInfo = RuleExpressionWalker.Validate(validation, binaryExpr.Left, false); } if (binaryExpr.Right == null) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, binaryExpr.Operator.ToString()); error = new ValidationError(message, ErrorNumbers.Error_RightOperandMissing); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } else { if (binaryExpr.Right is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, binaryExpr.Right.GetType().FullName); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr.Right; validation.AddError(error); return null; } rhsExprInfo = RuleExpressionWalker.Validate(validation, binaryExpr.Right, false); } validation.PopParentExpression(); RuleBinaryExpressionInfo resultExprInfo = null; if (lhsExprInfo != null && rhsExprInfo != null) { Type lhsType = lhsExprInfo.ExpressionType; Type rhsType = rhsExprInfo.ExpressionType; switch (binaryExpr.Operator) { case CodeBinaryOperatorType.Add: case CodeBinaryOperatorType.Subtract: case CodeBinaryOperatorType.Multiply: case CodeBinaryOperatorType.Divide: case CodeBinaryOperatorType.Modulus: case CodeBinaryOperatorType.BitwiseAnd: case CodeBinaryOperatorType.BitwiseOr: resultExprInfo = ArithmeticLiteral.ResultType(binaryExpr.Operator, lhsType, binaryExpr.Left, rhsType, binaryExpr.Right, validation, out error); if (resultExprInfo == null) { // check if constants are used with ulongs, as we should do some extra "conversions" if (((lhsType == typeof(ulong)) && (PromotionPossible(rhsType, binaryExpr.Right))) || ((rhsType == typeof(ulong)) && (PromotionPossible(lhsType, binaryExpr.Left)))) { resultExprInfo = new RuleBinaryExpressionInfo(lhsType, rhsType, typeof(ulong)); } else { error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } } break; case CodeBinaryOperatorType.IdentityEquality: case CodeBinaryOperatorType.IdentityInequality: resultExprInfo = new RuleBinaryExpressionInfo(lhsType, rhsType, typeof(bool)); break; case CodeBinaryOperatorType.ValueEquality: resultExprInfo = Literal.AllowedComparison(lhsType, binaryExpr.Left, rhsType, binaryExpr.Right, binaryExpr.Operator, validation, out error); if (resultExprInfo == null) { // check if constants are used with ulongs, as we should do some extra "conversions" if (((lhsType == typeof(ulong)) && (PromotionPossible(rhsType, binaryExpr.Right))) || ((rhsType == typeof(ulong)) && (PromotionPossible(lhsType, binaryExpr.Left)))) { resultExprInfo = new RuleBinaryExpressionInfo(lhsType, rhsType, typeof(bool)); } else { error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } } break; case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: resultExprInfo = Literal.AllowedComparison(lhsType, binaryExpr.Left, rhsType, binaryExpr.Right, binaryExpr.Operator, validation, out error); if (resultExprInfo == null) { // check if constants are used with ulongs, as we should do some extra "conversions" if (((lhsType == typeof(ulong)) && (PromotionPossible(rhsType, binaryExpr.Right))) || ((rhsType == typeof(ulong)) && (PromotionPossible(lhsType, binaryExpr.Left)))) { resultExprInfo = new RuleBinaryExpressionInfo(lhsType, rhsType, typeof(bool)); } else { error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } } break; case CodeBinaryOperatorType.BooleanAnd: case CodeBinaryOperatorType.BooleanOr: resultExprInfo = new RuleBinaryExpressionInfo(lhsType, rhsType, typeof(bool)); if (lhsType != typeof(bool)) { message = string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeLHS, binaryExpr.Operator.ToString(), (lhsType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(lhsType)); error = new ValidationError(message, ErrorNumbers.Error_LeftOperandInvalidType); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); resultExprInfo = null; } if (rhsType != typeof(bool)) { message = string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeRHS, binaryExpr.Operator.ToString(), (rhsType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhsType)); error = new ValidationError(message, ErrorNumbers.Error_RightOperandInvalidType); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); resultExprInfo = null; } break; default: { message = string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, binaryExpr.Operator.ToString()); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = binaryExpr; validation.Errors.Add(error); } break; } } // Validate any RuleAttributes, if present. if (resultExprInfo != null) { MethodInfo method = resultExprInfo.MethodInfo; if (method != null) { object[] attrs = method.GetCustomAttributes(typeof(RuleAttribute), true); if (attrs != null && attrs.Length > 0) { Stack<MemberInfo> methodStack = new Stack<MemberInfo>(); methodStack.Push(method); bool allAttributesValid = true; foreach (RuleAttribute ruleAttr in attrs) { if (!ruleAttr.Validate(validation, method, method.DeclaringType, method.GetParameters())) allAttributesValid = false; } methodStack.Pop(); if (!allAttributesValid) return null; } } } return resultExprInfo; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeDirectionExpression directionExpr = (CodeDirectionExpression)expression; if (isWritten) { string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeDirectionExpression).ToString()); ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget); error.UserData[RuleUserDataKeys.ErrorObject] = directionExpr; validation.Errors.Add(error); return null; } // direction specified, make sure that something is specified if (directionExpr.Expression == null) { ValidationError error = new ValidationError(Messages.NullDirectionTarget, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = directionExpr; validation.Errors.Add(error); return null; } if (directionExpr.Expression is CodeTypeReferenceExpression) { string message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, directionExpr.Expression.GetType().FullName); ValidationError error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = directionExpr.Expression; validation.AddError(error); return null; } // validate the parameter RuleExpressionInfo paramExprInfo; bool isRef; if (directionExpr.Direction == FieldDirection.Ref) { // ref parameters require that we both read and write the value isRef = true; paramExprInfo = RuleExpressionWalker.Validate(validation, directionExpr.Expression, false); if (paramExprInfo == null) return null; paramExprInfo = RuleExpressionWalker.Validate(validation, directionExpr.Expression, true); } else if (directionExpr.Direction == FieldDirection.Out) { // out parameters mean that we only write to it isRef = true; paramExprInfo = RuleExpressionWalker.Validate(validation, directionExpr.Expression, true); } else { // other parameters are treated as in, so we need to be able to read them isRef = false; paramExprInfo = RuleExpressionWalker.Validate(validation, directionExpr.Expression, false); } if (paramExprInfo == null) return null; // determine it's type Type parameterType = paramExprInfo.ExpressionType; if (parameterType == null) return null; if (parameterType != typeof(NullLiteral)) { // adjust type if necessary if (isRef && !parameterType.IsByRef) parameterType = parameterType.MakeByRefType(); } return new RuleExpressionInfo(parameterType); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { Type targetType = null; RuleMethodInvokeExpressionInfo methodInvokeInfo = null; string message; ValidationError error = null; BindingFlags bindingFlags = BindingFlags.Public; CodeMethodInvokeExpression invokeExpr = (CodeMethodInvokeExpression)expression; if (isWritten) { message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeMethodInvokeExpression).ToString()); error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); return null; } if ((invokeExpr.Method == null) || (invokeExpr.Method.TargetObject == null)) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, invokeExpr.Method.MethodName); error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); return null; // Fatal error; discontinue validation of this object. } if ((invokeExpr.Method.TypeArguments != null) && (invokeExpr.Method.TypeArguments.Count > 0)) { error = new ValidationError(Messages.GenericMethodsNotSupported, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); return null; } try { // Early exit from this if a cycle is detected. if (!validation.PushParentExpression(invokeExpr)) return null; RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, invokeExpr.Method.TargetObject, false); if (targetExprInfo == null) // error occurred, so simply return return null; targetType = targetExprInfo.ExpressionType; if (targetType == null) return null; // if an error occurred (targetType == null), continue on to validate the arguments if (targetType == typeof(NullLiteral)) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, invokeExpr.Method.MethodName); error = new ValidationError(message, ErrorNumbers.Error_BindingTypeMissing); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); targetType = null; // force exit after validating the arguments } List<CodeExpression> argExprs = new List<CodeExpression>(); bool hasInvalidArgument = false; if (invokeExpr.Parameters != null) { for (int i = 0; i < invokeExpr.Parameters.Count; ++i) { CodeExpression argExpr = invokeExpr.Parameters[i]; if (argExpr == null) { message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, i.ToString(CultureInfo.CurrentCulture), invokeExpr.Method.MethodName); error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); targetType = null; // force exit after validating the rest of the arguments } else { if (argExpr is CodeTypeReferenceExpression) { message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, argExpr.GetType().FullName); error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = argExpr; validation.AddError(error); hasInvalidArgument = true; } // Validate the argument. RuleExpressionInfo argExprInfo = RuleExpressionWalker.Validate(validation, argExpr, false); if (argExprInfo == null) hasInvalidArgument = true; argExprs.Add(argExpr); } } } // Stop further validation if there was a problem with the target expression. if (targetType == null) return null; // Stop further validation if there was a problem with any of the arguments. if (hasInvalidArgument) return null; if (invokeExpr.Method.TargetObject is CodeTypeReferenceExpression) bindingFlags |= BindingFlags.Static | BindingFlags.FlattenHierarchy; else bindingFlags |= BindingFlags.Instance; if (validation.AllowInternalMembers(targetType)) bindingFlags |= BindingFlags.NonPublic; // Everything okay so far, try to resolve the method. methodInvokeInfo = validation.ResolveMethod(targetType, invokeExpr.Method.MethodName, bindingFlags, argExprs, out error); if ((methodInvokeInfo == null) && (invokeExpr.UserData.Contains(RuleUserDataKeys.QualifiedName))) { // failed to resolve the method, but a fully qualified type name is around // load the type, add it to the assemblies, and try again string qualifiedName = invokeExpr.UserData[RuleUserDataKeys.QualifiedName] as string; Type containingClassType = validation.ResolveType(qualifiedName); if (containingClassType != null) { validation.DetermineExtensionMethods(containingClassType.Assembly); methodInvokeInfo = validation.ResolveMethod(targetType, invokeExpr.Method.MethodName, bindingFlags, argExprs, out error); } } if (methodInvokeInfo == null) { error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); return null; } } finally { validation.PopParentExpression(); } MethodInfo mi = methodInvokeInfo.MethodInfo; if (mi.ReturnType == null) { // This can only happen with a design-time type. message = string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, invokeExpr.Method.MethodName); error = new ValidationError(message, ErrorNumbers.Error_CouldNotDetermineMemberType); error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr; validation.Errors.Add(error); return null; } if (!validation.ValidateMemberAccess(invokeExpr.Method.TargetObject, targetType, mi, invokeExpr.Method.MethodName, invokeExpr)) return null; // Validate any RuleAttributes, if present. object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true); if (attrs != null && attrs.Length > 0) { Stack<MemberInfo> methodStack = new Stack<MemberInfo>(); methodStack.Push(mi); bool allAttributesValid = true; foreach (RuleAttribute ruleAttr in attrs) { if (!ruleAttr.Validate(validation, mi, targetType, mi.GetParameters())) allAttributesValid = false; } methodStack.Pop(); if (!allAttributesValid) return null; } // if this is an extension method, save the type information if (mi is ExtensionMethodInfo) { invokeExpr.UserData[RuleUserDataKeys.QualifiedName] = mi.DeclaringType.AssemblyQualifiedName; } return methodInvokeInfo; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError error; CodeBinaryOperatorExpression newParent = (CodeBinaryOperatorExpression)expression; if (!validation.PushParentExpression(newParent)) { return(null); } if (isWritten) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeBinaryOperatorExpression).ToString() }), 0x17a); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } RuleExpressionInfo info = null; RuleExpressionInfo info2 = null; if (newParent.Left == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { newParent.Operator.ToString() }), 0x541); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Left is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Left.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Left; validation.AddError(error); return(null); } info = RuleExpressionWalker.Validate(validation, newParent.Left, false); } if (newParent.Right == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { newParent.Operator.ToString() }), 0x543); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Right is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Right.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Right; validation.AddError(error); return(null); } info2 = RuleExpressionWalker.Validate(validation, newParent.Right, false); } validation.PopParentExpression(); RuleBinaryExpressionInfo info3 = null; if ((info != null) && (info2 != null)) { Type expressionType = info.ExpressionType; Type rhs = info2.ExpressionType; switch (newParent.Operator) { case CodeBinaryOperatorType.Add: case CodeBinaryOperatorType.Subtract: case CodeBinaryOperatorType.Multiply: case CodeBinaryOperatorType.Divide: case CodeBinaryOperatorType.Modulus: case CodeBinaryOperatorType.BitwiseOr: case CodeBinaryOperatorType.BitwiseAnd: info3 = ArithmeticLiteral.ResultType(newParent.Operator, expressionType, newParent.Left, rhs, newParent.Right, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(ulong)); } } goto Label_063E; case CodeBinaryOperatorType.IdentityInequality: case CodeBinaryOperatorType.IdentityEquality: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); goto Label_063E; case CodeBinaryOperatorType.ValueEquality: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; case CodeBinaryOperatorType.BooleanOr: case CodeBinaryOperatorType.BooleanAnd: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); if (expressionType != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeLHS, new object[] { newParent.Operator.ToString(), (expressionType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(expressionType) }), 0x542); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } if (rhs != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeRHS, new object[] { newParent.Operator.ToString(), (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs) }), 0x544); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } goto Label_063E; case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; } error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { newParent.Operator.ToString() }), 0x548); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } Label_063E: if (info3 != null) { MethodInfo methodInfo = info3.MethodInfo; if (methodInfo == null) { return(info3); } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes == null) || (customAttributes.Length <= 0)) { return(info3); } Stack <MemberInfo> stack = new Stack <MemberInfo>(); stack.Push(methodInfo); bool flag = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, methodInfo, methodInfo.DeclaringType, methodInfo.GetParameters())) { flag = false; } } stack.Pop(); if (!flag) { return(null); } } return(info3); }
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters) { ValidationError error = null; if (string.IsNullOrEmpty(this.attributePath)) { return true; } string[] strArray = this.attributePath.Split(new char[] { '/' }); string str2 = strArray[0]; int num = 0; if (this.attributeTarget == RuleAttributeTarget.This) { if (str2 == "this") { num++; } } else { bool flag2 = false; for (int j = 0; j < parameters.Length; j++) { ParameterInfo info = parameters[j]; if (info.Name == str2) { flag2 = true; contextType = info.ParameterType; break; } } if (!flag2) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, new object[] { str2, member.Name }), 420); error.UserData["ErrorObject"] = this; validation.AddError(error); return false; } num++; } int length = strArray.Length; string str3 = strArray[length - 1]; if (string.IsNullOrEmpty(str3) || (str3 == "*")) { length--; } Type fieldType = contextType; for (int i = num; i < length; i++) { if (!(strArray[i] == "*")) { goto Label_0171; } error = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195); error.UserData["ErrorObject"] = this; validation.AddError(error); return false; Label_0168: fieldType = fieldType.GetElementType(); Label_0171: if (fieldType.IsArray) { goto Label_0168; } BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; if (validation.AllowInternalMembers(fieldType)) { bindingAttr |= BindingFlags.NonPublic; } FieldInfo field = fieldType.GetField(strArray[i], bindingAttr); if (field != null) { fieldType = field.FieldType; } else { PropertyInfo property = fieldType.GetProperty(strArray[i], bindingAttr); if (property != null) { fieldType = property.PropertyType; } else { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 390); error.UserData["ErrorObject"] = this; validation.AddError(error); return false; } } } return true; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { Type expressionType = null; RuleMethodInvokeExpressionInfo info = null; ValidationError item = null; BindingFlags @public = BindingFlags.Public; CodeMethodInvokeExpression newParent = (CodeMethodInvokeExpression) expression; if (isWritten) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeMethodInvokeExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if ((newParent.Method == null) || (newParent.Method.TargetObject == null)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if ((newParent.Method.TypeArguments != null) && (newParent.Method.TypeArguments.Count > 0)) { item = new ValidationError(Messages.GenericMethodsNotSupported, 0x548); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } try { if (!validation.PushParentExpression(newParent)) { return null; } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.Method.TargetObject, false); if (info2 == null) { return null; } expressionType = info2.ExpressionType; if (expressionType == null) { return null; } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x546); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } List<CodeExpression> argumentExprs = new List<CodeExpression>(); bool flag = false; if (newParent.Parameters != null) { for (int i = 0; i < newParent.Parameters.Count; i++) { CodeExpression expression3 = newParent.Parameters[i]; if (expression3 == null) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), newParent.Method.MethodName }), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } else { if (expression3 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression3.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression3; validation.AddError(item); flag = true; } if (RuleExpressionWalker.Validate(validation, expression3, false) == null) { flag = true; } argumentExprs.Add(expression3); } } } if (expressionType == null) { return null; } if (flag) { return null; } if (newParent.Method.TargetObject is CodeTypeReferenceExpression) { @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static; } else { @public |= BindingFlags.Instance; } if (validation.AllowInternalMembers(expressionType)) { @public |= BindingFlags.NonPublic; } info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item); if ((info == null) && newParent.UserData.Contains("QualifiedName")) { string qualifiedName = newParent.UserData["QualifiedName"] as string; Type type2 = validation.ResolveType(qualifiedName); if (type2 != null) { validation.DetermineExtensionMethods(type2.Assembly); info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item); } } if (info == null) { item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } } finally { validation.PopParentExpression(); } MethodInfo methodInfo = info.MethodInfo; if (methodInfo.ReturnType == null) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.Method.MethodName }), 0x194); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if (!validation.ValidateMemberAccess(newParent.Method.TargetObject, expressionType, methodInfo, newParent.Method.MethodName, newParent)) { return null; } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { Stack<MemberInfo> stack = new Stack<MemberInfo>(); stack.Push(methodInfo); bool flag2 = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, methodInfo, expressionType, methodInfo.GetParameters())) { flag2 = false; } } stack.Pop(); if (!flag2) { return null; } } if (methodInfo is ExtensionMethodInfo) { newParent.UserData["QualifiedName"] = methodInfo.DeclaringType.AssemblyQualifiedName; } return info; }
private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack<MemberInfo> methodStack) { string message; ValidationError error; if (string.IsNullOrEmpty(methodInvoked)) { // Invoked method or property name was null or empty. message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, Messages.NullValue); error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); return false; } bool valid = true; // Go through all the methods and properties on the target context, // looking for all the ones that match the name on the attribute. MemberInfo[] members = contextType.GetMember(methodInvoked, MemberTypes.Method | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); if (members == null || members.Length == 0) { // Invoked method or property didn't exist. message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, methodInvoked); error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; } else { for (int i = 0; i < members.Length; ++i) { MemberInfo mi = members[i]; if (!methodStack.Contains(mi)) // Prevent recursion { methodStack.Push(mi); object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true); if (attrs != null && attrs.Length != 0) { foreach (RuleAttribute invokedRuleAttr in attrs) { RuleReadWriteAttribute readWriteAttr = invokedRuleAttr as RuleReadWriteAttribute; if (readWriteAttr != null) { // This read/write attribute may not specify a target of "Parameter", since // we can't map from the invoker's parameters to the invokee's parameters. if (readWriteAttr.Target == RuleAttributeTarget.Parameter) { message = string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, mi.Name); error = new ValidationError(message, ErrorNumbers.Error_InvokeAttrRefersToParameterAttribute, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; } else { // Validate the read/write attribute normally. readWriteAttr.Validate(validation, mi, contextType, null); } } else { RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)invokedRuleAttr; invokeAttr.ValidateInvokeAttribute(validation, mi, contextType, methodStack); } } } methodStack.Pop(); } } } return valid; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError item = null; Type expressionType = null; CodeArrayIndexerExpression newParent = (CodeArrayIndexerExpression)expression; CodeExpression targetObject = newParent.TargetObject; if (targetObject == null) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (targetObject is CodeTypeReferenceExpression) { item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if ((newParent.Indices == null) || (newParent.Indices.Count == 0)) { item = new ValidationError(Messages.MissingIndexExpressions, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } try { if (!validation.PushParentExpression(newParent)) { return(null); } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); if (info == null) { return(null); } expressionType = info.ExpressionType; if (expressionType == null) { return(null); } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (!expressionType.IsArray) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotIndexType, new object[] { RuleDecompiler.DecompileType(expressionType) }), 0x19b); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } int arrayRank = expressionType.GetArrayRank(); if (newParent.Indices.Count != arrayRank) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadRank, new object[] { arrayRank }), 0x19c); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } bool flag = false; for (int i = 0; i < newParent.Indices.Count; i++) { CodeExpression expression4 = newParent.Indices[i]; if (expression4 == null) { item = new ValidationError(Messages.NullIndexExpression, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag = true; continue; } if (expression4 is CodeDirectionExpression) { item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d); item.UserData["ErrorObject"] = expression4; validation.Errors.Add(item); flag = true; } if (expression4 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression4; validation.AddError(item); flag = true; } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, expression4, false); if (info2 != null) { Type type = info2.ExpressionType; switch (Type.GetTypeCode(type)) { case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.Int64: { continue; } } item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadType, new object[] { RuleDecompiler.DecompileType(type) }), 0x19e); item.UserData["ErrorObject"] = expression4; validation.Errors.Add(item); flag = true; continue; } flag = true; } if (flag) { return(null); } } finally { validation.PopParentExpression(); } return(new RuleExpressionInfo(expressionType.GetElementType())); }
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters) { ValidationError error = null; string message = null; if (string.IsNullOrEmpty(attributePath)) { // It is allowed to pass null or the empty string to [RuleRead] or [RuleWrite]. This // is how you indicate that a method or property has no dependencies or side effects. return true; } bool valid = true; string[] parts = attributePath.Split('/'); // Check the first part. string firstPart = parts[0]; int startOfRelativePortion = 0; if (attributeTarget == RuleAttributeTarget.This) { // When target is "This", the path is allowed to start with the token "this". It is // then skipped for the rest of the validation, and the contextType remains what it // was when passed in. if (firstPart == "this") ++startOfRelativePortion; } else { // When target is "Parameter", the path must start with the name of a parameter. bool found = false; for (int p = 0; p < parameters.Length; ++p) { ParameterInfo param = parameters[p]; if (param.Name == firstPart) { found = true; // The context type is the parameter type. contextType = param.ParameterType; break; } } if (!found) { message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, firstPart, member.Name); error = new ValidationError(message, ErrorNumbers.Error_InvalidRuleAttributeParameter); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); return false; } ++startOfRelativePortion; } int numParts = parts.Length; // Check the last part. The last part is allowed to be empty, or "*". string lastPart = parts[numParts - 1]; if (string.IsNullOrEmpty(lastPart) || lastPart == "*") numParts -= 1; // Check the rest of the parts. Type currentType = contextType; for (int i = startOfRelativePortion; i < numParts; ++i) { // Can't have embedded "*" wildcards. if (parts[i] == "*") { // The "*" occurred in the middle of the path, which is a no-no. error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; break; } // Skip array types. while (currentType.IsArray) currentType = currentType.GetElementType(); // Make sure the member exists in the current type. BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (validation.AllowInternalMembers(currentType)) bindingFlags |= BindingFlags.NonPublic; FieldInfo field = currentType.GetField(parts[i], bindingFlags); if (field != null) { currentType = field.FieldType; } else { PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags); if (property != null) { currentType = property.PropertyType; } else { message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]); error = new ValidationError(message, ErrorNumbers.Error_UnknownFieldOrProperty); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; break; } } } return valid; }
public bool Validate(RuleValidation validation) { if (validation == null) throw new ArgumentNullException("validation"); // Validate each rule. Dictionary<string, object> ruleNames = new Dictionary<string, object>(); foreach (Rule r in rules) { if (!string.IsNullOrEmpty(r.Name)) // invalid names caught when validating the rule { if (ruleNames.ContainsKey(r.Name)) { // Duplicate rule name found. ValidationError error = new ValidationError(Messages.Error_DuplicateRuleName, ErrorNumbers.Error_DuplicateConditions); error.UserData[RuleUserDataKeys.ErrorObject] = r; validation.AddError(error); } else { ruleNames.Add(r.Name, null); } } r.Validate(validation); } if (validation.Errors == null || validation.Errors.Count == 0) return true; return false; }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } if (this.codeDomStatement == null) { ValidationError error = new ValidationError(Messages.NullStatement, 0x53d); error.UserData["ErrorObject"] = this; validator.AddError(error); return false; } return CodeDomStatementWalker.Validate(validator, this.codeDomStatement); }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } bool success = true; if (path == null) { ValidationError error = new ValidationError(Messages.NullUpdate, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; } // now make sure that the path is valid string[] parts = path.Split('/'); if (parts[0] == "this") { Type currentType = validator.ThisType; for (int i = 1; i < parts.Length; ++i) { if (parts[i] == "*") { if (i < parts.Length - 1) { // The "*" occurred in the middle of the path, which is a no-no. ValidationError error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; break; } else { // It occurred at the end, which is okay. break; } } else if (string.IsNullOrEmpty(parts[i]) && i == parts.Length - 1) { // It's okay to end with a "/". break; } while (currentType.IsArray) { currentType = currentType.GetElementType(); } BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (validator.AllowInternalMembers(currentType)) { bindingFlags |= BindingFlags.NonPublic; } FieldInfo field = currentType.GetField(parts[i], bindingFlags); if (field != null) { currentType = field.FieldType; } else { PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags); if (property != null) { currentType = property.PropertyType; } else { string message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]); ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidUpdate); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; break; } } } } else { ValidationError error = new ValidationError(Messages.UpdateNotThis, ErrorNumbers.Error_InvalidUpdate); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; } return(success); }
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters) { ValidationError error = null; if (string.IsNullOrEmpty(this.attributePath)) { return(true); } string[] strArray = this.attributePath.Split(new char[] { '/' }); string str2 = strArray[0]; int num = 0; if (this.attributeTarget == RuleAttributeTarget.This) { if (str2 == "this") { num++; } } else { bool flag2 = false; for (int j = 0; j < parameters.Length; j++) { ParameterInfo info = parameters[j]; if (info.Name == str2) { flag2 = true; contextType = info.ParameterType; break; } } if (!flag2) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, new object[] { str2, member.Name }), 420); error.UserData["ErrorObject"] = this; validation.AddError(error); return(false); } num++; } int length = strArray.Length; string str3 = strArray[length - 1]; if (string.IsNullOrEmpty(str3) || (str3 == "*")) { length--; } Type fieldType = contextType; for (int i = num; i < length; i++) { if (!(strArray[i] == "*")) { goto Label_0171; } error = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195); error.UserData["ErrorObject"] = this; validation.AddError(error); return(false); Label_0168: fieldType = fieldType.GetElementType(); Label_0171: if (fieldType.IsArray) { goto Label_0168; } BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; if (validation.AllowInternalMembers(fieldType)) { bindingAttr |= BindingFlags.NonPublic; } FieldInfo field = fieldType.GetField(strArray[i], bindingAttr); if (field != null) { fieldType = field.FieldType; } else { PropertyInfo property = fieldType.GetProperty(strArray[i], bindingAttr); if (property != null) { fieldType = property.PropertyType; } else { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 390); error.UserData["ErrorObject"] = this; validation.AddError(error); return(false); } } } return(true); }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } bool flag = true; if (this.path == null) { ValidationError error = new ValidationError(Messages.NullUpdate, 0x53d); error.UserData["ErrorObject"] = this; validator.AddError(error); flag = false; } string[] strArray = this.path.Split(new char[] { '/' }); if (strArray[0] == "this") { Type thisType = validator.ThisType; for (int i = 1; i < strArray.Length; i++) { if (strArray[i] == "*") { if (i < (strArray.Length - 1)) { ValidationError error2 = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195); error2.UserData["ErrorObject"] = this; validator.AddError(error2); flag = false; } return(flag); } if (!string.IsNullOrEmpty(strArray[i]) || (i != (strArray.Length - 1))) { goto Label_00ED; } return(flag); Label_00E6: thisType = thisType.GetElementType(); Label_00ED: if (thisType.IsArray) { goto Label_00E6; } BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; if (validator.AllowInternalMembers(thisType)) { bindingAttr |= BindingFlags.NonPublic; } FieldInfo field = thisType.GetField(strArray[i], bindingAttr); if (field != null) { thisType = field.FieldType; } else { PropertyInfo property = thisType.GetProperty(strArray[i], bindingAttr); if (property != null) { thisType = property.PropertyType; } else { ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 0x57b); error3.UserData["ErrorObject"] = this; validator.AddError(error3); return(false); } } } return(flag); } ValidationError error4 = new ValidationError(Messages.UpdateNotThis, 0x57b); error4.UserData["ErrorObject"] = this; validator.AddError(error4); return(false); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { Type expressionType = null; RuleMethodInvokeExpressionInfo info = null; ValidationError item = null; BindingFlags @public = BindingFlags.Public; CodeMethodInvokeExpression newParent = (CodeMethodInvokeExpression)expression; if (isWritten) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeMethodInvokeExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if ((newParent.Method == null) || (newParent.Method.TargetObject == null)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if ((newParent.Method.TypeArguments != null) && (newParent.Method.TypeArguments.Count > 0)) { item = new ValidationError(Messages.GenericMethodsNotSupported, 0x548); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } try { if (!validation.PushParentExpression(newParent)) { return(null); } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.Method.TargetObject, false); if (info2 == null) { return(null); } expressionType = info2.ExpressionType; if (expressionType == null) { return(null); } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x546); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } List <CodeExpression> argumentExprs = new List <CodeExpression>(); bool flag = false; if (newParent.Parameters != null) { for (int i = 0; i < newParent.Parameters.Count; i++) { CodeExpression expression3 = newParent.Parameters[i]; if (expression3 == null) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), newParent.Method.MethodName }), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } else { if (expression3 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression3.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression3; validation.AddError(item); flag = true; } if (RuleExpressionWalker.Validate(validation, expression3, false) == null) { flag = true; } argumentExprs.Add(expression3); } } } if (expressionType == null) { return(null); } if (flag) { return(null); } if (newParent.Method.TargetObject is CodeTypeReferenceExpression) { @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static; } else { @public |= BindingFlags.Instance; } if (validation.AllowInternalMembers(expressionType)) { @public |= BindingFlags.NonPublic; } info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item); if ((info == null) && newParent.UserData.Contains("QualifiedName")) { string qualifiedName = newParent.UserData["QualifiedName"] as string; Type type2 = validation.ResolveType(qualifiedName); if (type2 != null) { validation.DetermineExtensionMethods(type2.Assembly); info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item); } } if (info == null) { item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } } finally { validation.PopParentExpression(); } MethodInfo methodInfo = info.MethodInfo; if (methodInfo.ReturnType == null) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.Method.MethodName }), 0x194); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (!validation.ValidateMemberAccess(newParent.Method.TargetObject, expressionType, methodInfo, newParent.Method.MethodName, newParent)) { return(null); } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { Stack <MemberInfo> stack = new Stack <MemberInfo>(); stack.Push(methodInfo); bool flag2 = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, methodInfo, expressionType, methodInfo.GetParameters())) { flag2 = false; } } stack.Pop(); if (!flag2) { return(null); } } if (methodInfo is ExtensionMethodInfo) { newParent.UserData["QualifiedName"] = methodInfo.DeclaringType.AssemblyQualifiedName; } return(info); }
private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack <MemberInfo> methodStack) { string message; ValidationError error; if (string.IsNullOrEmpty(methodInvoked)) { // Invoked method or property name was null or empty. message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, Messages.NullValue); error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); return(false); } bool valid = true; // Go through all the methods and properties on the target context, // looking for all the ones that match the name on the attribute. MemberInfo[] members = contextType.GetMember(methodInvoked, MemberTypes.Method | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); if (members == null || members.Length == 0) { // Invoked method or property didn't exist. message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, methodInvoked); error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; } else { for (int i = 0; i < members.Length; ++i) { MemberInfo mi = members[i]; if (!methodStack.Contains(mi)) // Prevent recursion { methodStack.Push(mi); object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true); if (attrs != null && attrs.Length != 0) { foreach (RuleAttribute invokedRuleAttr in attrs) { if (invokedRuleAttr is RuleReadWriteAttribute readWriteAttr) { // This read/write attribute may not specify a target of "Parameter", since // we can't map from the invoker's parameters to the invokee's parameters. if (readWriteAttr.Target == RuleAttributeTarget.Parameter) { message = string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, mi.Name); error = new ValidationError(message, ErrorNumbers.Error_InvokeAttrRefersToParameterAttribute, true); error.UserData[RuleUserDataKeys.ErrorObject] = this; validation.AddError(error); valid = false; } else { // Validate the read/write attribute normally. readWriteAttr.Validate(validation, mi, contextType, null); } } else { RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)invokedRuleAttr; invokeAttr.ValidateInvokeAttribute(validation, mi, contextType, methodStack); } } } methodStack.Pop(); } } } return(valid); }
public override bool Validate(RuleValidation validation) { if (validation == null) { throw new ArgumentNullException("validation"); } bool flag = true; if (this._expression == null) { flag = false; ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ConditionExpressionNull, new object[] { typeof(CodePrimitiveExpression).ToString() }), 400); error.UserData["ErrorObject"] = this; validation.AddError(error); return flag; } return validation.ValidateConditionExpression(this._expression); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError error; CodeBinaryOperatorExpression newParent = (CodeBinaryOperatorExpression) expression; if (!validation.PushParentExpression(newParent)) { return null; } if (isWritten) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeBinaryOperatorExpression).ToString() }), 0x17a); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } RuleExpressionInfo info = null; RuleExpressionInfo info2 = null; if (newParent.Left == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { newParent.Operator.ToString() }), 0x541); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Left is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Left.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Left; validation.AddError(error); return null; } info = RuleExpressionWalker.Validate(validation, newParent.Left, false); } if (newParent.Right == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { newParent.Operator.ToString() }), 0x543); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Right is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Right.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Right; validation.AddError(error); return null; } info2 = RuleExpressionWalker.Validate(validation, newParent.Right, false); } validation.PopParentExpression(); RuleBinaryExpressionInfo info3 = null; if ((info != null) && (info2 != null)) { Type expressionType = info.ExpressionType; Type rhs = info2.ExpressionType; switch (newParent.Operator) { case CodeBinaryOperatorType.Add: case CodeBinaryOperatorType.Subtract: case CodeBinaryOperatorType.Multiply: case CodeBinaryOperatorType.Divide: case CodeBinaryOperatorType.Modulus: case CodeBinaryOperatorType.BitwiseOr: case CodeBinaryOperatorType.BitwiseAnd: info3 = ArithmeticLiteral.ResultType(newParent.Operator, expressionType, newParent.Left, rhs, newParent.Right, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(ulong)); } } goto Label_063E; case CodeBinaryOperatorType.IdentityInequality: case CodeBinaryOperatorType.IdentityEquality: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); goto Label_063E; case CodeBinaryOperatorType.ValueEquality: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; case CodeBinaryOperatorType.BooleanOr: case CodeBinaryOperatorType.BooleanAnd: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); if (expressionType != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeLHS, new object[] { newParent.Operator.ToString(), (expressionType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(expressionType) }), 0x542); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } if (rhs != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeRHS, new object[] { newParent.Operator.ToString(), (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs) }), 0x544); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } goto Label_063E; case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; } error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { newParent.Operator.ToString() }), 0x548); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } Label_063E: if (info3 != null) { MethodInfo methodInfo = info3.MethodInfo; if (methodInfo == null) { return info3; } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes == null) || (customAttributes.Length <= 0)) { return info3; } Stack<MemberInfo> stack = new Stack<MemberInfo>(); stack.Push(methodInfo); bool flag = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, methodInfo, methodInfo.DeclaringType, methodInfo.GetParameters())) { flag = false; } } stack.Pop(); if (!flag) { return null; } } return info3; }
public override bool Validate(RuleValidation validator) { if (validator == null) throw new ArgumentNullException("validator"); bool success = true; if (path == null) { ValidationError error = new ValidationError(Messages.NullUpdate, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; } // now make sure that the path is valid string[] parts = path.Split('/'); if (parts[0] == "this") { Type currentType = validator.ThisType; for (int i = 1; i < parts.Length; ++i) { if (parts[i] == "*") { if (i < parts.Length - 1) { // The "*" occurred in the middle of the path, which is a no-no. ValidationError error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; break; } else { // It occurred at the end, which is okay. break; } } else if (string.IsNullOrEmpty(parts[i]) && i == parts.Length - 1) { // It's okay to end with a "/". break; } while (currentType.IsArray) currentType = currentType.GetElementType(); BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (validator.AllowInternalMembers(currentType)) bindingFlags |= BindingFlags.NonPublic; FieldInfo field = currentType.GetField(parts[i], bindingFlags); if (field != null) { currentType = field.FieldType; } else { PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags); if (property != null) { currentType = property.PropertyType; } else { string message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]); ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidUpdate); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; break; } } } } else { ValidationError error = new ValidationError(Messages.UpdateNotThis, ErrorNumbers.Error_InvalidUpdate); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); success = false; } return success; }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeCastExpression expression2 = (CodeCastExpression) expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeCastExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = expression2; validation.Errors.Add(item); return null; } if (expression2.Expression == null) { ValidationError error2 = new ValidationError(Messages.NullCastExpr, 0x53d); error2.UserData["ErrorObject"] = expression2; validation.Errors.Add(error2); return null; } if (expression2.Expression is CodeTypeReferenceExpression) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = expression2.Expression; validation.AddError(error); return null; } if (expression2.TargetType == null) { ValidationError error4 = new ValidationError(Messages.NullCastType, 0x53d); error4.UserData["ErrorObject"] = expression2; validation.Errors.Add(error4); return null; } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, expression2.Expression, false); if (info == null) { return null; } Type expressionType = info.ExpressionType; Type type = validation.ResolveType(expression2.TargetType); if (type == null) { return null; } if (expressionType == typeof(NullLiteral)) { if (ConditionHelper.IsNonNullableValueType(type)) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, new object[] { RuleDecompiler.DecompileType(type) }), 0x53d); error5.UserData["ErrorObject"] = expression2; validation.Errors.Add(error5); return null; } } else { Type type3 = expressionType; if (ConditionHelper.IsNullableValueType(type3)) { type3 = type3.GetGenericArguments()[0]; } Type type4 = type; if (ConditionHelper.IsNullableValueType(type4)) { type4 = type4.GetGenericArguments()[0]; } bool flag = false; if (type3.IsValueType && type4.IsValueType) { if (type3.IsEnum) { flag = type4.IsEnum || IsNumeric(type4); } else if (type4.IsEnum) { flag = IsNumeric(type3); } else if (type3 == typeof(char)) { flag = IsNumeric(type4); } else if (type4 == typeof(char)) { flag = IsNumeric(type3); } else if (type3.IsPrimitive && type4.IsPrimitive) { try { Convert.ChangeType(Activator.CreateInstance(type3), type4, CultureInfo.CurrentCulture); flag = true; } catch (Exception) { flag = false; } } } if (!flag) { ValidationError error6; flag = RuleValidation.ExplicitConversionSpecified(expressionType, type, out error6); if (error6 != null) { error6.UserData["ErrorObject"] = expression2; validation.Errors.Add(error6); return null; } } if (!flag) { ValidationError error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(type) }), 0x53d); error7.UserData["ErrorObject"] = expression2; validation.Errors.Add(error7); return null; } } return new RuleExpressionInfo(type); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError item = null; RulePropertyExpressionInfo info = null; bool nonPublic = false; Type expressionType = null; CodeIndexerExpression newParent = (CodeIndexerExpression)expression; CodeExpression targetObject = newParent.TargetObject; if (targetObject == null) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (targetObject is CodeTypeReferenceExpression) { item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if ((newParent.Indices == null) || (newParent.Indices.Count == 0)) { item = new ValidationError(Messages.MissingIndexExpressions, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } try { if (!validation.PushParentExpression(newParent)) { return(null); } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); if (info2 == null) { return(null); } expressionType = info2.ExpressionType; if (expressionType == null) { return(null); } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); expressionType = null; } List <CodeExpression> argumentExprs = new List <CodeExpression>(); bool flag2 = false; for (int i = 0; i < newParent.Indices.Count; i++) { CodeExpression expression4 = newParent.Indices[i]; if (expression4 == null) { item = new ValidationError(Messages.NullIndexExpression, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag2 = true; } else { CodeDirectionExpression expression5 = expression4 as CodeDirectionExpression; if ((expression5 != null) && (expression5.Direction != FieldDirection.In)) { item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag2 = true; } if (expression4 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression4; validation.AddError(item); flag2 = true; } if (RuleExpressionWalker.Validate(validation, expression4, false) == null) { flag2 = true; } else { argumentExprs.Add(expression4); } } } if (expressionType == null) { return(null); } if (flag2) { return(null); } BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance; if (validation.AllowInternalMembers(expressionType)) { bindingFlags |= BindingFlags.NonPublic; nonPublic = true; } info = validation.ResolveIndexerProperty(expressionType, bindingFlags, argumentExprs, out item); if (info == null) { item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } } finally { validation.PopParentExpression(); } PropertyInfo propertyInfo = info.PropertyInfo; MethodInfo accessorMethod = isWritten ? propertyInfo.GetSetMethod(nonPublic) : propertyInfo.GetGetMethod(nonPublic); if (accessorMethod == null) { string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet; item = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { propertyInfo.Name, RuleDecompiler.DecompileType(expressionType) }), 0x54a); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (!validation.ValidateMemberAccess(targetObject, expressionType, accessorMethod, propertyInfo.Name, newParent)) { return(null); } object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { Stack <MemberInfo> stack = new Stack <MemberInfo>(); stack.Push(propertyInfo); bool flag3 = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, propertyInfo, expressionType, propertyInfo.GetIndexParameters())) { flag3 = false; } } stack.Pop(); if (!flag3) { return(null); } } return(info); }
public override bool Validate(RuleValidation validator) { if (validator == null) throw new ArgumentNullException("validator"); if (codeDomStatement == null) { ValidationError error = new ValidationError(Messages.NullStatement, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = this; validator.AddError(error); return false; } else { return CodeDomStatementWalker.Validate(validator, codeDomStatement); } }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError item = null; Type expressionType = null; CodeArrayIndexerExpression newParent = (CodeArrayIndexerExpression) expression; CodeExpression targetObject = newParent.TargetObject; if (targetObject == null) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if (targetObject is CodeTypeReferenceExpression) { item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if ((newParent.Indices == null) || (newParent.Indices.Count == 0)) { item = new ValidationError(Messages.MissingIndexExpressions, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } try { if (!validation.PushParentExpression(newParent)) { return null; } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); if (info == null) { return null; } expressionType = info.ExpressionType; if (expressionType == null) { return null; } if (expressionType == typeof(NullLiteral)) { item = new ValidationError(Messages.NullIndexerTarget, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } if (!expressionType.IsArray) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotIndexType, new object[] { RuleDecompiler.DecompileType(expressionType) }), 0x19b); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } int arrayRank = expressionType.GetArrayRank(); if (newParent.Indices.Count != arrayRank) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadRank, new object[] { arrayRank }), 0x19c); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return null; } bool flag = false; for (int i = 0; i < newParent.Indices.Count; i++) { CodeExpression expression4 = newParent.Indices[i]; if (expression4 == null) { item = new ValidationError(Messages.NullIndexExpression, 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); flag = true; continue; } if (expression4 is CodeDirectionExpression) { item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d); item.UserData["ErrorObject"] = expression4; validation.Errors.Add(item); flag = true; } if (expression4 is CodeTypeReferenceExpression) { item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression4; validation.AddError(item); flag = true; } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, expression4, false); if (info2 != null) { Type type = info2.ExpressionType; switch (Type.GetTypeCode(type)) { case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.Int64: { continue; } } item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadType, new object[] { RuleDecompiler.DecompileType(type) }), 0x19e); item.UserData["ErrorObject"] = expression4; validation.Errors.Add(item); flag = true; continue; } flag = true; } if (flag) { return null; } } finally { validation.PopParentExpression(); } return new RuleExpressionInfo(expressionType.GetElementType()); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeCastExpression expression2 = (CodeCastExpression)expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeCastExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = expression2; validation.Errors.Add(item); return(null); } if (expression2.Expression == null) { ValidationError error2 = new ValidationError(Messages.NullCastExpr, 0x53d); error2.UserData["ErrorObject"] = expression2; validation.Errors.Add(error2); return(null); } if (expression2.Expression is CodeTypeReferenceExpression) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = expression2.Expression; validation.AddError(error); return(null); } if (expression2.TargetType == null) { ValidationError error4 = new ValidationError(Messages.NullCastType, 0x53d); error4.UserData["ErrorObject"] = expression2; validation.Errors.Add(error4); return(null); } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, expression2.Expression, false); if (info == null) { return(null); } Type expressionType = info.ExpressionType; Type type = validation.ResolveType(expression2.TargetType); if (type == null) { return(null); } if (expressionType == typeof(NullLiteral)) { if (ConditionHelper.IsNonNullableValueType(type)) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, new object[] { RuleDecompiler.DecompileType(type) }), 0x53d); error5.UserData["ErrorObject"] = expression2; validation.Errors.Add(error5); return(null); } } else { Type type3 = expressionType; if (ConditionHelper.IsNullableValueType(type3)) { type3 = type3.GetGenericArguments()[0]; } Type type4 = type; if (ConditionHelper.IsNullableValueType(type4)) { type4 = type4.GetGenericArguments()[0]; } bool flag = false; if (type3.IsValueType && type4.IsValueType) { if (type3.IsEnum) { flag = type4.IsEnum || IsNumeric(type4); } else if (type4.IsEnum) { flag = IsNumeric(type3); } else if (type3 == typeof(char)) { flag = IsNumeric(type4); } else if (type4 == typeof(char)) { flag = IsNumeric(type3); } else if (type3.IsPrimitive && type4.IsPrimitive) { try { Convert.ChangeType(Activator.CreateInstance(type3), type4, CultureInfo.CurrentCulture); flag = true; } catch (Exception) { flag = false; } } } if (!flag) { ValidationError error6; flag = RuleValidation.ExplicitConversionSpecified(expressionType, type, out error6); if (error6 != null) { error6.UserData["ErrorObject"] = expression2; validation.Errors.Add(error6); return(null); } } if (!flag) { ValidationError error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(type) }), 0x53d); error7.UserData["ErrorObject"] = expression2; validation.Errors.Add(error7); return(null); } } return(new RuleExpressionInfo(type)); }
public override bool Validate(RuleValidation validator) { if (validator == null) { throw new ArgumentNullException("validator"); } bool flag = true; if (this.path == null) { ValidationError error = new ValidationError(Messages.NullUpdate, 0x53d); error.UserData["ErrorObject"] = this; validator.AddError(error); flag = false; } string[] strArray = this.path.Split(new char[] { '/' }); if (strArray[0] == "this") { Type thisType = validator.ThisType; for (int i = 1; i < strArray.Length; i++) { if (strArray[i] == "*") { if (i < (strArray.Length - 1)) { ValidationError error2 = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195); error2.UserData["ErrorObject"] = this; validator.AddError(error2); flag = false; } return flag; } if (!string.IsNullOrEmpty(strArray[i]) || (i != (strArray.Length - 1))) { goto Label_00ED; } return flag; Label_00E6: thisType = thisType.GetElementType(); Label_00ED: if (thisType.IsArray) { goto Label_00E6; } BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; if (validator.AllowInternalMembers(thisType)) { bindingAttr |= BindingFlags.NonPublic; } FieldInfo field = thisType.GetField(strArray[i], bindingAttr); if (field != null) { thisType = field.FieldType; } else { PropertyInfo property = thisType.GetProperty(strArray[i], bindingAttr); if (property != null) { thisType = property.PropertyType; } else { ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 0x57b); error3.UserData["ErrorObject"] = this; validator.AddError(error3); return false; } } } return flag; } ValidationError error4 = new ValidationError(Messages.UpdateNotThis, 0x57b); error4.UserData["ErrorObject"] = this; validator.AddError(error4); return false; }