internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution) { CodeCastExpression expression2 = (CodeCastExpression)expression; object operandValue = RuleExpressionWalker.Evaluate(execution, expression2.Expression).Value; RuleExpressionInfo info = execution.Validation.ExpressionInfo(expression2); if (info == null) { InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0])); exception.Data["ErrorObject"] = expression2; throw exception; } Type expressionType = info.ExpressionType; if (operandValue == null) { if (ConditionHelper.IsNonNullableValueType(expressionType)) { RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { Messages.NullValue, RuleDecompiler.DecompileType(expressionType) })); exception2.Data["ErrorObject"] = expression2; throw exception2; } } else { operandValue = Executor.AdjustTypeWithCast(execution.Validation.ExpressionInfo(expression2.Expression).ExpressionType, operandValue, expressionType); } return(new RuleLiteralResult(operandValue)); }
public static RuleExpressionInfo Validate(RuleValidation validation, CodeExpression expression, bool isWritten) { if (validation == null) { throw new ArgumentNullException("validation"); } // See if we've visited this node before. // Always check if written = true RuleExpressionInfo resultExprInfo = null; if (!isWritten) { resultExprInfo = validation.ExpressionInfo(expression); } if (resultExprInfo == null) { // First time we've seen this node. RuleExpressionInternal ruleExpr = GetExpression(expression); if (ruleExpr == null) { string message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, expression.GetType().FullName); ValidationError error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = expression; if (validation.Errors == null) { string typeName = string.Empty; if ((validation.ThisType != null) && (validation.ThisType.Name != null)) { typeName = validation.ThisType.Name; } string exceptionMessage = string.Format( CultureInfo.CurrentCulture, Messages.ErrorsCollectionMissing, typeName); throw new InvalidOperationException(exceptionMessage); } else { validation.Errors.Add(error); } return(null); } resultExprInfo = validation.ValidateSubexpression(expression, ruleExpr, isWritten); } return(resultExprInfo); }
public static RuleExpressionInfo Validate(RuleValidation validation, CodeExpression expression, bool isWritten) { if (validation == null) { throw new ArgumentNullException("validation"); } RuleExpressionInfo info = null; if (!isWritten) { info = validation.ExpressionInfo(expression); } if (info != null) { return(info); } RuleExpressionInternal ruleExpr = GetExpression(expression); if (ruleExpr == null) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression.GetType().FullName }), 0x548); item.UserData["ErrorObject"] = expression; if (validation.Errors == null) { string name = string.Empty; if ((validation.ThisType != null) && (validation.ThisType.Name != null)) { name = validation.ThisType.Name; } throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ErrorsCollectionMissing, new object[] { name })); } validation.Errors.Add(item); return(null); } return(validation.ValidateSubexpression(expression, ruleExpr, isWritten)); }
internal override bool Validate(RuleValidation validation) { bool success = false; if (exprStatement.Expression == null) { ValidationError error = new ValidationError(Messages.NullInvokeStatementExpression, ErrorNumbers.Error_ParameterNotSet); error.UserData[RuleUserDataKeys.ErrorObject] = exprStatement; validation.Errors.Add(error); } else if (exprStatement.Expression is CodeMethodInvokeExpression) { RuleExpressionInfo exprInfo = RuleExpressionWalker.Validate(validation, exprStatement.Expression, false); success = (exprInfo != null); } else { ValidationError error = new ValidationError(Messages.InvokeNotHandled, ErrorNumbers.Error_CodeExpressionNotHandled); error.UserData[RuleUserDataKeys.ErrorObject] = exprStatement; validation.Errors.Add(error); } 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 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) { bool success = false; string message; RuleExpressionInfo lhsExprInfo = null; if (assignStatement.Left == null) { ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandMissing); error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement; validation.Errors.Add(error); } else { lhsExprInfo = validation.ExpressionInfo(assignStatement.Left); if (lhsExprInfo == null) { lhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Left, true); } } RuleExpressionInfo rhsExprInfo = null; if (assignStatement.Right == null) { ValidationError error = new ValidationError(Messages.NullAssignRight, ErrorNumbers.Error_RightOperandMissing); error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement; validation.Errors.Add(error); } else { rhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Right, false); } if (lhsExprInfo != null && rhsExprInfo != null) { Type expressionType = rhsExprInfo.ExpressionType; Type assignmentType = lhsExprInfo.ExpressionType; if (assignmentType == typeof(NullLiteral)) { // Can't assign to a null literal. ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandInvalidType); error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement; validation.Errors.Add(error); success = false; } else if (assignmentType == expressionType) { // Easy case, they're both the same type. success = true; } else { // The types aren't the same, but it still might be a legal assignment. if (!RuleValidation.TypesAreAssignable(expressionType, assignmentType, assignStatement.Right, out ValidationError error)) { if (error == null) { message = string.Format(CultureInfo.CurrentCulture, Messages.AssignNotAllowed, RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(assignmentType)); error = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible); } error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement; validation.Errors.Add(error); } else { success = true; } } } return(success); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeFieldReferenceExpression newParent = (CodeFieldReferenceExpression)expression; if (newParent.TargetObject == null) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { newParent.FieldName }), 0x53d); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (!validation.PushParentExpression(newParent)) { return(null); } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); validation.PopParentExpression(); if (info == null) { return(null); } Type expressionType = info.ExpressionType; if (expressionType == null) { return(null); } if (expressionType == typeof(NullLiteral)) { ValidationError error2 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { newParent.FieldName }), 0x546); error2.UserData["ErrorObject"] = newParent; validation.Errors.Add(error2); return(null); } BindingFlags @public = BindingFlags.Public; if (newParent.TargetObject is CodeTypeReferenceExpression) { @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static; } else { @public |= BindingFlags.Instance; } if (validation.AllowInternalMembers(expressionType)) { @public |= BindingFlags.NonPublic; } FieldInfo field = expressionType.GetField(newParent.FieldName, @public); if (field == null) { ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownField, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x54a); error3.UserData["ErrorObject"] = newParent; validation.Errors.Add(error3); return(null); } if (field.FieldType == null) { ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.FieldName }), 0x194); error4.UserData["ErrorObject"] = newParent; validation.Errors.Add(error4); return(null); } if (isWritten && field.IsLiteral) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.FieldSetNotAllowed, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x17a); error5.UserData["ErrorObject"] = newParent; validation.Errors.Add(error5); return(null); } if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, field, field.Name, newParent)) { return(null); } validation.IsAuthorized(field.FieldType); return(new RuleFieldExpressionInfo(field)); }
internal override bool Validate(RuleValidation validation) { bool flag = false; RuleExpressionInfo info = null; if (this.assignStatement.Left == null) { ValidationError item = new ValidationError(Messages.NullAssignLeft, 0x541); item.UserData["ErrorObject"] = this.assignStatement; validation.Errors.Add(item); } else { info = validation.ExpressionInfo(this.assignStatement.Left); if (info == null) { info = RuleExpressionWalker.Validate(validation, this.assignStatement.Left, true); } } RuleExpressionInfo info2 = null; if (this.assignStatement.Right == null) { ValidationError error2 = new ValidationError(Messages.NullAssignRight, 0x543); error2.UserData["ErrorObject"] = this.assignStatement; validation.Errors.Add(error2); } else { info2 = RuleExpressionWalker.Validate(validation, this.assignStatement.Right, false); } if ((info == null) || (info2 == null)) { return(flag); } Type expressionType = info2.ExpressionType; Type lhsType = info.ExpressionType; if (lhsType == typeof(NullLiteral)) { ValidationError error3 = new ValidationError(Messages.NullAssignLeft, 0x542); error3.UserData["ErrorObject"] = this.assignStatement; validation.Errors.Add(error3); return(false); } if (lhsType != expressionType) { ValidationError error = null; if (!RuleValidation.TypesAreAssignable(expressionType, lhsType, this.assignStatement.Right, out error)) { if (error == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AssignNotAllowed, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(lhsType) }), 0x545); } error.UserData["ErrorObject"] = this.assignStatement; validation.Errors.Add(error); return(flag); } } return(true); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeArrayCreateExpression newParent = (CodeArrayCreateExpression)expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeObjectCreateExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = newParent; validation.Errors.Add(item); return(null); } if (newParent.CreateType == null) { ValidationError error2 = new ValidationError(Messages.NullTypeType, 0x53d); error2.UserData["ErrorObject"] = newParent; validation.Errors.Add(error2); return(null); } Type lhsType = validation.ResolveType(newParent.CreateType); if (lhsType == null) { return(null); } if (lhsType.IsArray) { ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayTypeInvalid, new object[] { lhsType.Name }), 0x53d); error3.UserData["ErrorObject"] = newParent; validation.Errors.Add(error3); return(null); } try { if (!validation.PushParentExpression(newParent)) { return(null); } if (newParent.Size < 0) { ValidationError error4 = new ValidationError(Messages.ArraySizeInvalid, 0x53d); error4.UserData["ErrorObject"] = newParent; validation.Errors.Add(error4); return(null); } if (newParent.SizeExpression != null) { RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.SizeExpression, false); if (info == null) { return(null); } if (((info.ExpressionType != typeof(int)) && (info.ExpressionType != typeof(uint))) && ((info.ExpressionType != typeof(long)) && (info.ExpressionType != typeof(ulong)))) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArraySizeTypeInvalid, new object[] { info.ExpressionType.Name }), 0x53d); error5.UserData["ErrorObject"] = newParent; validation.Errors.Add(error5); return(null); } } bool flag = false; for (int i = 0; i < newParent.Initializers.Count; i++) { CodeExpression expression3 = newParent.Initializers[i]; if (expression3 == null) { ValidationError error6 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.MissingInitializer, new object[] { lhsType.Name }), 0x53d); error6.UserData["ErrorObject"] = newParent; validation.Errors.Add(error6); return(null); } RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, expression3, false); if (info2 == null) { flag = true; } else { ValidationError error7; if (!RuleValidation.StandardImplicitConversion(info2.ExpressionType, lhsType, expression3, out error7)) { if (error7 != null) { error7.UserData["ErrorObject"] = newParent; validation.Errors.Add(error7); } error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InitializerMismatch, new object[] { i, lhsType.Name }), 0x545); error7.UserData["ErrorObject"] = newParent; validation.Errors.Add(error7); return(null); } } } if (flag) { return(null); } double size = -1.0; if (newParent.SizeExpression != null) { CodePrimitiveExpression sizeExpression = newParent.SizeExpression as CodePrimitiveExpression; if ((sizeExpression != null) && (sizeExpression.Value != null)) { size = (double)Executor.AdjustType(sizeExpression.Value.GetType(), sizeExpression.Value, typeof(double)); } if (newParent.Size > 0) { ValidationError error8 = new ValidationError(Messages.ArraySizeBoth, 0x53d); error8.UserData["ErrorObject"] = newParent; validation.Errors.Add(error8); return(null); } } else if (newParent.Size > 0) { size = newParent.Size; } if ((size >= 0.0) && (newParent.Initializers.Count > size)) { ValidationError error9 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InitializerCountMismatch, new object[] { newParent.Initializers.Count, size }), 0x545); error9.UserData["ErrorObject"] = newParent; validation.Errors.Add(error9); return(null); } } finally { validation.PopParentExpression(); } return(new RuleExpressionInfo(lhsType.MakeArrayType())); }
internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution) { CodeArrayCreateExpression expression2 = (CodeArrayCreateExpression)expression; if (expression2.CreateType == null) { RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType); exception.Data["ErrorObject"] = expression2; throw exception; } RuleExpressionInfo info = execution.Validation.ExpressionInfo(expression2); if (expression2 == null) { InvalidOperationException exception2 = new InvalidOperationException(Messages.ExpressionNotValidated); exception2.Data["ErrorObject"] = expression2; throw exception2; } Type elementType = info.ExpressionType.GetElementType(); int length = 0; if (expression2.SizeExpression != null) { Type expressionType = execution.Validation.ExpressionInfo(expression2.SizeExpression).ExpressionType; RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.SizeExpression); if (expressionType == typeof(int)) { length = (int)result.Value; } else if (expressionType == typeof(long)) { length = (int)((long)result.Value); } else if (expressionType == typeof(uint)) { length = (int)((uint)result.Value); } else if (expressionType == typeof(ulong)) { length = (int)((ulong)result.Value); } } else if (expression2.Size != 0) { length = expression2.Size; } else { length = expression2.Initializers.Count; } Array literal = Array.CreateInstance(elementType, length); if (expression2.Initializers != null) { for (int i = 0; i < expression2.Initializers.Count; i++) { CodeExpression expression3 = expression2.Initializers[i]; Type operandType = execution.Validation.ExpressionInfo(expression3).ExpressionType; RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression3); literal.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), i); } } return(new RuleLiteralResult(literal)); }
internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution) { object obj2; CodeObjectCreateExpression expression2 = (CodeObjectCreateExpression)expression; if (expression2.CreateType == null) { RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType); exception.Data["ErrorObject"] = expression2; throw exception; } RuleExpressionInfo info = execution.Validation.ExpressionInfo(expression2); if (info == null) { InvalidOperationException exception2 = new InvalidOperationException(Messages.ExpressionNotValidated); exception2.Data["ErrorObject"] = expression2; throw exception2; } RuleConstructorExpressionInfo info2 = info as RuleConstructorExpressionInfo; if (info2 == null) { return(new RuleLiteralResult(Activator.CreateInstance(info.ExpressionType))); } ConstructorInfo constructorInfo = info2.ConstructorInfo; object[] parameters = null; RuleExpressionResult[] resultArray = null; if ((expression2.Parameters != null) && (expression2.Parameters.Count > 0)) { int count = expression2.Parameters.Count; ParameterInfo[] infoArray = constructorInfo.GetParameters(); parameters = new object[infoArray.Length]; int length = infoArray.Length; if (info2.NeedsParamsExpansion) { length--; } int index = 0; while (index < length) { Type expressionType = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType; RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]); CodeDirectionExpression expression3 = expression2.Parameters[index] as CodeDirectionExpression; if ((expression3 != null) && ((expression3.Direction == FieldDirection.Ref) || (expression3.Direction == FieldDirection.Out))) { if (resultArray == null) { resultArray = new RuleExpressionResult[count]; } resultArray[index] = result; } parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType); index++; } if (length < count) { ParameterInfo info4 = infoArray[length]; Type elementType = info4.ParameterType.GetElementType(); Array array = Array.CreateInstance(elementType, (int)(count - index)); while (index < count) { Type operandType = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType; RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]); array.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), (int)(index - length)); index++; } parameters[length] = array; } } try { obj2 = constructorInfo.Invoke(parameters); } catch (TargetInvocationException exception3) { if (exception3.InnerException == null) { throw; } throw new TargetInvocationException(string.Format(CultureInfo.CurrentCulture, Messages.Error_ConstructorInvoke, new object[] { RuleDecompiler.DecompileType(info2.ExpressionType), exception3.InnerException.Message }), exception3.InnerException); } if (resultArray != null) { for (int i = 0; i < expression2.Parameters.Count; i++) { if (resultArray[i] != null) { resultArray[i].Value = parameters[i]; } } } return(new RuleLiteralResult(obj2)); }
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); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodePropertyReferenceExpression newParent = (CodePropertyReferenceExpression)expression; if (newParent.TargetObject == null) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x53d); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); return(null); } if (!validation.PushParentExpression(newParent)) { return(null); } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false); validation.PopParentExpression(); if (info == null) { return(null); } Type expressionType = info.ExpressionType; if (expressionType == null) { return(null); } if (expressionType == typeof(NullLiteral)) { ValidationError error2 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x546); error2.UserData["ErrorObject"] = newParent; validation.Errors.Add(error2); return(null); } bool nonPublic = false; BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; if (validation.AllowInternalMembers(expressionType)) { bindingFlags |= BindingFlags.NonPublic; nonPublic = true; } PropertyInfo item = validation.ResolveProperty(expressionType, newParent.PropertyName, bindingFlags); if (item == null) { ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownProperty, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a); error3.UserData["ErrorObject"] = newParent; validation.Errors.Add(error3); return(null); } if (item.PropertyType == null) { ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.PropertyName }), 0x194); error4.UserData["ErrorObject"] = newParent; validation.Errors.Add(error4); return(null); } MethodInfo accessorMethod = isWritten ? item.GetSetMethod(nonPublic) : item.GetGetMethod(nonPublic); if (accessorMethod == null) { string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet; ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a); error5.UserData["ErrorObject"] = newParent; validation.Errors.Add(error5); return(null); } if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, accessorMethod, newParent.PropertyName, newParent)) { return(null); } object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes != null) && (customAttributes.Length > 0)) { Stack <MemberInfo> stack = new Stack <MemberInfo>(); stack.Push(item); bool flag2 = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, item, expressionType, null)) { flag2 = false; } } stack.Pop(); if (!flag2) { return(null); } } return(new RulePropertyExpressionInfo(item, item.PropertyType, false)); }
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) { 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); }