internal override void AnalyzeUsage(RuleAnalysis analysis) { RuleExpressionWalker.AnalyzeUsage(analysis, this.assignStatement.Left, false, true, null); RuleExpressionWalker.AnalyzeUsage(analysis, this.assignStatement.Right, true, false, null); }
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 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) { 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) { 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) { 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 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 void AnalyzeReadWrite(RuleAnalysis analysis, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions) { if (string.IsNullOrEmpty(this.attributePath)) { if (this.attributeTarget == RuleAttributeTarget.This) { attributedExpressions.Add(targetExpression); } else if (this.attributeTarget == RuleAttributeTarget.Parameter) { for (int i = 0; i < argumentExpressions.Count; i++) { attributedExpressions.Add(argumentExpressions[i]); } } } else { string attributePath = this.attributePath; bool isRead = !analysis.ForWrites; bool forWrites = analysis.ForWrites; if (this.attributeTarget == RuleAttributeTarget.This) { string str2 = "this/"; if (attributePath.StartsWith(str2, StringComparison.Ordinal)) { attributePath = attributePath.Substring(str2.Length); } RuleExpressionWalker.AnalyzeUsage(analysis, targetExpression, isRead, forWrites, new RulePathQualifier(attributePath, targetQualifier)); attributedExpressions.Add(targetExpression); } else if (this.attributeTarget == RuleAttributeTarget.Parameter) { string paramName = null; int index = attributePath.IndexOf('/'); if (index >= 0) { paramName = attributePath.Substring(0, index); attributePath = attributePath.Substring(index + 1); } else { paramName = attributePath; attributePath = null; } ParameterInfo info = Array.Find <ParameterInfo>(parameters, p => p.Name == paramName); if (info != null) { RulePathQualifier qualifier = string.IsNullOrEmpty(attributePath) ? null : new RulePathQualifier(attributePath, null); int count = info.Position + 1; if (info.Position == (parameters.Length - 1)) { count = argumentExpressions.Count; } for (int j = info.Position; j < count; j++) { CodeExpression expression = argumentExpressions[j]; RuleExpressionWalker.AnalyzeUsage(analysis, expression, isRead, forWrites, qualifier); attributedExpressions.Add(expression); } } } } }
internal override void Execute(RuleExecution execution) { RuleExpressionWalker.Evaluate(execution, this.exprStatement.Expression); }
internal override bool Match(CodeStatement comperand) { CodeExpressionStatement statement = comperand as CodeExpressionStatement; return((statement != null) && RuleExpressionWalker.Match(this.exprStatement.Expression, statement.Expression)); }
internal override CodeStatement Clone() { return(new CodeExpressionStatement { Expression = RuleExpressionWalker.Clone(this.exprStatement.Expression) }); }
internal override void AnalyzeUsage(RuleAnalysis analysis) { RuleExpressionWalker.AnalyzeUsage(analysis, this.exprStatement.Expression, false, false, null); }
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. ValidationError error = null; if (!RuleValidation.TypesAreAssignable(expressionType, assignmentType, assignStatement.Right, out 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 CodeStatement Clone() { return(new CodeAssignStatement { Left = RuleExpressionWalker.Clone(this.assignStatement.Left), Right = RuleExpressionWalker.Clone(this.assignStatement.Right) }); }
internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution) { object obj3; CodeMethodInvokeExpression expression2 = (CodeMethodInvokeExpression)expression; object obj2 = RuleExpressionWalker.Evaluate(execution, expression2.Method.TargetObject).Value; RuleMethodInvokeExpressionInfo info = execution.Validation.ExpressionInfo(expression2) as RuleMethodInvokeExpressionInfo; if (info == null) { InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0])); exception.Data["ErrorObject"] = expression2; throw exception; } MethodInfo methodInfo = info.MethodInfo; if (!methodInfo.IsStatic && (obj2 == null)) { RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullMethod, new object[] { expression2.Method.MethodName })); exception2.Data["ErrorObject"] = expression2; throw exception2; } object[] parameters = null; RuleExpressionResult[] resultArray = null; if ((expression2.Parameters != null) && (expression2.Parameters.Count > 0)) { int count = expression2.Parameters.Count; ParameterInfo[] infoArray = methodInfo.GetParameters(); parameters = new object[infoArray.Length]; int length = infoArray.Length; if (info.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[expression2.Parameters.Count]; } resultArray[index] = result; if (expression3.Direction != FieldDirection.Out) { parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType); } } else { parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType); } index++; } if (length < count) { ParameterInfo info3 = infoArray[length]; Type parameterType = info3.ParameterType; Type elementType = parameterType.GetElementType(); Array array = (Array)parameterType.InvokeMember(parameterType.Name, BindingFlags.CreateInstance, null, null, new object[] { count - index }, CultureInfo.CurrentCulture); 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 { obj3 = methodInfo.Invoke(obj2, parameters); } catch (TargetInvocationException exception3) { if (exception3.InnerException == null) { throw; } throw new TargetInvocationException(string.Format(CultureInfo.CurrentCulture, Messages.Error_MethodInvoke, new object[] { RuleDecompiler.DecompileType(methodInfo.ReflectedType), methodInfo.Name, 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(obj3)); }
internal override bool Match(CodeStatement comperand) { CodeAssignStatement statement = comperand as CodeAssignStatement; return(((statement != null) && RuleExpressionWalker.Match(this.assignStatement.Left, statement.Left)) && RuleExpressionWalker.Match(this.assignStatement.Right, statement.Right)); }
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); }
public override bool Equals(object obj) { bool flag = false; RuleExpressionCondition condition = obj as RuleExpressionCondition; if (condition != null) { flag = (this.Name == condition.Name) && (((this._expression == null) && (condition.Expression == null)) || ((this._expression != null) && RuleExpressionWalker.Match(this._expression, condition.Expression))); } return(flag); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError error; CodeObjectCreateExpression newParent = (CodeObjectCreateExpression)expression; if (isWritten) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeObjectCreateExpression).ToString() }), 0x17a); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); return(null); } if (newParent.CreateType == null) { error = new ValidationError(Messages.NullTypeType, 0x53d); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); return(null); } Type type = validation.ResolveType(newParent.CreateType); if (type == null) { return(null); } List <CodeExpression> argumentExprs = new List <CodeExpression>(); try { if (!validation.PushParentExpression(newParent)) { return(null); } bool flag = false; for (int i = 0; i < newParent.Parameters.Count; i++) { CodeExpression expression3 = newParent.Parameters[i]; if (expression3 == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), RuleDecompiler.DecompileType(type) }), 0x53d); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); flag = true; } else { if (RuleExpressionWalker.Validate(validation, expression3, false) == null) { flag = true; } argumentExprs.Add(expression3); } } if (flag) { return(null); } } finally { validation.PopParentExpression(); } BindingFlags constructorBindingFlags = BindingFlags.Public | BindingFlags.Instance; if (validation.AllowInternalMembers(type)) { constructorBindingFlags |= BindingFlags.NonPublic; } if (type.IsValueType && (argumentExprs.Count == 0)) { return(new RuleExpressionInfo(type)); } if (type.IsAbstract) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); return(null); } RuleConstructorExpressionInfo info2 = validation.ResolveConstructor(type, constructorBindingFlags, argumentExprs, out error); if (info2 == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); return(null); } return(info2); }
internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution) { CodeDirectionExpression expression2 = (CodeDirectionExpression)expression; return(RuleExpressionWalker.Evaluate(execution, expression2.Expression)); }
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 void AnalyzeReadWrite(RuleAnalysis analysis, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions) { if (string.IsNullOrEmpty(attributePath)) { // If the suffix is null or empty, this means the RuleAttributeTarget has no dependencies. if (attributeTarget == RuleAttributeTarget.This) { // The target object has no dependencies. attributedExpressions.Add(targetExpression); } else if (attributeTarget == RuleAttributeTarget.Parameter) { // ALL arguments have no dependencies. for (int i = 0; i < argumentExpressions.Count; ++i) { attributedExpressions.Add(argumentExpressions[i]); } } } else { string suffix = attributePath; bool isRead = !analysis.ForWrites; bool isWrite = analysis.ForWrites; if (attributeTarget == RuleAttributeTarget.This) { // Target is "This", so perform the analysis on the target expression. // Remove the optional "this/" token if present. string optionalPrefix = "this/"; if (suffix.StartsWith(optionalPrefix, StringComparison.Ordinal)) { suffix = suffix.Substring(optionalPrefix.Length); } RuleExpressionWalker.AnalyzeUsage(analysis, targetExpression, isRead, isWrite, new RulePathQualifier(suffix, targetQualifier)); attributedExpressions.Add(targetExpression); } else if (attributeTarget == RuleAttributeTarget.Parameter) { string paramName = null; int firstSlash = suffix.IndexOf('/'); if (firstSlash >= 0) { paramName = suffix.Substring(0, firstSlash); suffix = suffix.Substring(firstSlash + 1); } else { paramName = suffix; suffix = null; } // Find the ParameterInfo that corresponds to this attribute path. ParameterInfo param = Array.Find <ParameterInfo>(parameters, delegate(ParameterInfo p) { return(p.Name == paramName); }); if (param != null) { RulePathQualifier qualifier = string.IsNullOrEmpty(suffix) ? null : new RulePathQualifier(suffix, null); // 99.9% of the time, the parameter usage attribute only applies to one argument. However, // if this attribute corresponds to the last parameter, then just assume that all the trailing // arguments correspond. (In other words, if the caller passed more arguments then there // are parameters, we assume it was a params array.) // // Usually this loop will only execute once. int end = param.Position + 1; if (param.Position == parameters.Length - 1) { end = argumentExpressions.Count; } for (int i = param.Position; i < end; ++i) { CodeExpression argExpr = argumentExpressions[i]; RuleExpressionWalker.AnalyzeUsage(analysis, argExpr, isRead, isWrite, qualifier); attributedExpressions.Add(argExpr); } } } } }
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); }