private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack<MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
 {
     foreach (MemberInfo info in contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
     {
         if (!methodStack.Contains(info))
         {
             methodStack.Push(info);
             object[] customAttributes = info.GetCustomAttributes(typeof(RuleAttribute), true);
             if ((customAttributes != null) && (customAttributes.Length != 0))
             {
                 foreach (RuleAttribute attribute in (RuleAttribute[]) customAttributes)
                 {
                     RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute;
                     if (attribute2 != null)
                     {
                         attribute2.Analyze(analysis, info, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                     }
                     else
                     {
                         ((RuleInvokeAttribute) attribute).AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                     }
                 }
             }
             methodStack.Pop();
         }
     }
 }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeDirectionExpression expression2 = (CodeDirectionExpression) expression;
            CodeExpression expression3 = expression2.Expression;
            bool flag = false;
            bool flag2 = true;
            RulePathQualifier qualifier2 = null;
            switch (expression2.Direction)
            {
                case FieldDirection.In:
                    flag = false;
                    flag2 = true;
                    qualifier2 = new RulePathQualifier("*", null);
                    break;

                case FieldDirection.Out:
                    flag = true;
                    flag2 = false;
                    qualifier2 = null;
                    break;

                case FieldDirection.Ref:
                    flag = true;
                    flag2 = true;
                    qualifier2 = analysis.ForWrites ? null : new RulePathQualifier("*", null);
                    break;
            }
            RuleExpressionWalker.AnalyzeUsage(analysis, expression3, flag2, flag, qualifier2);
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            if (analysis.ForWrites && !isWritten)            // If we're tracking writes, then ignore things that aren't written.
                return;
            else if (!analysis.ForWrites && !isRead)   // ... and vice-versa
                return;

            StringBuilder sb = new StringBuilder("this/");
            for (RulePathQualifier q = qualifier; q != null; q = q.Next)
            {
                sb.Append(q.Name);
                if (q.Name == "*")
                {
                    if (q.Next != null)
                        throw new NotSupportedException(Messages.InvalidWildCardInPathQualifier);
                }
                else
                {
                    sb.Append("/");
                }
            }

            // Add the symbol to our set.
            analysis.AddSymbol(sb.ToString());
        }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodePropertyReferenceExpression expression2 = (CodePropertyReferenceExpression) expression;
     CodeExpression targetObject = expression2.TargetObject;
     if (analysis.Validation.ExpressionInfo(targetObject) == null)
     {
         InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
         exception.Data["ErrorObject"] = targetObject;
         throw exception;
     }
     RulePropertyExpressionInfo info2 = analysis.Validation.ExpressionInfo(expression2) as RulePropertyExpressionInfo;
     if (info2 == null)
     {
         InvalidOperationException exception2 = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
         exception2.Data["ErrorObject"] = expression2;
         throw exception2;
     }
     PropertyInfo propertyInfo = info2.PropertyInfo;
     List<CodeExpression> attributedExprs = new List<CodeExpression>();
     analysis.AnalyzeRuleAttributes(propertyInfo, targetObject, qualifier, null, null, attributedExprs);
     if (!attributedExprs.Contains(targetObject))
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, new RulePathQualifier(propertyInfo.Name, qualifier));
     }
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeIndexerExpression expression2 = (CodeIndexerExpression) expression;
     CodeExpression targetObject = expression2.TargetObject;
     if (analysis.Validation.ExpressionInfo(targetObject) == null)
     {
         InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
         exception.Data["ErrorObject"] = targetObject;
         throw exception;
     }
     RulePropertyExpressionInfo info2 = analysis.Validation.ExpressionInfo(expression2) as RulePropertyExpressionInfo;
     if (info2 == null)
     {
         InvalidOperationException exception2 = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
         exception2.Data["ErrorObject"] = expression2;
         throw exception2;
     }
     PropertyInfo propertyInfo = info2.PropertyInfo;
     List<CodeExpression> attributedExprs = new List<CodeExpression>();
     analysis.AnalyzeRuleAttributes(propertyInfo, targetObject, qualifier, expression2.Indices, propertyInfo.GetIndexParameters(), attributedExprs);
     if (!attributedExprs.Contains(targetObject))
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, qualifier);
     }
     for (int i = 0; i < expression2.Indices.Count; i++)
     {
         CodeExpression item = expression2.Indices[i];
         if (!attributedExprs.Contains(item))
         {
             RuleExpressionWalker.AnalyzeUsage(analysis, item, true, false, null);
         }
     }
 }
 internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
 {
     if (!analysis.ForWrites)
     {
         base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
     }
 }
 internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
 {
     Stack<MemberInfo> methodStack = new Stack<MemberInfo>();
     methodStack.Push(member);
     this.AnalyzeInvokeAttribute(analysis, member.DeclaringType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
     methodStack.Pop();
 }
 public static void AnalyzeUsage(RuleAnalysis analysis, CodeExpression expression, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     if (analysis == null)
     {
         throw new ArgumentNullException("analysis");
     }
     GetExpression(expression).AnalyzeUsage(expression, analysis, isRead, isWritten, qualifier);
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeObjectCreateExpression expression2 = (CodeObjectCreateExpression) expression;
     foreach (CodeExpression expression3 in expression2.Parameters)
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);
     }
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeArrayIndexerExpression expression2 = (CodeArrayIndexerExpression) expression;
     RuleExpressionWalker.AnalyzeUsage(analysis, expression2.TargetObject, isRead, isWritten, qualifier);
     for (int i = 0; i < expression2.Indices.Count; i++)
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Indices[i], true, false, null);
     }
 }
 internal void AnalyzeRuleAttributes(MemberInfo member, CodeExpression targetExpr, RulePathQualifier targetQualifier, CodeExpressionCollection argExprs, ParameterInfo[] parameters, List<CodeExpression> attributedExprs)
 {
     object[] attrs = member.GetCustomAttributes(typeof(RuleAttribute), true);
     if (attrs != null && attrs.Length > 0)
     {
         RuleAttribute[] ruleAttrs = (RuleAttribute[])attrs;
         for (int i = 0; i < ruleAttrs.Length; ++i)
             ruleAttrs[i].Analyze(this, member, targetExpr, targetQualifier, argExprs, parameters, attributedExprs);
     }
 }
 internal void AnalyzeRuleAttributes(MemberInfo member, CodeExpression targetExpr, RulePathQualifier targetQualifier, CodeExpressionCollection argExprs, ParameterInfo[] parameters, List<CodeExpression> attributedExprs)
 {
     object[] customAttributes = member.GetCustomAttributes(typeof(RuleAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         RuleAttribute[] attributeArray = (RuleAttribute[]) customAttributes;
         for (int i = 0; i < attributeArray.Length; i++)
         {
             attributeArray[i].Analyze(this, member, targetExpr, targetQualifier, argExprs, parameters, attributedExprs);
         }
     }
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeArrayCreateExpression expression2 = (CodeArrayCreateExpression) expression;
     if (expression2.SizeExpression != null)
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, expression2.SizeExpression, true, false, null);
     }
     foreach (CodeExpression expression3 in expression2.Initializers)
     {
         RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);
     }
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeBinaryOperatorExpression expression2 = (CodeBinaryOperatorExpression) expression;
     RuleBinaryExpressionInfo info = analysis.Validation.ExpressionInfo(expression2) as RuleBinaryExpressionInfo;
     if (info != null)
     {
         MethodInfo methodInfo = info.MethodInfo;
         if (methodInfo != null)
         {
             List<CodeExpression> attributedExprs = new List<CodeExpression>();
             CodeExpressionCollection argExprs = new CodeExpressionCollection();
             argExprs.Add(expression2.Left);
             argExprs.Add(expression2.Right);
             CodeExpression targetExpr = new CodeTypeReferenceExpression(methodInfo.DeclaringType);
             analysis.AnalyzeRuleAttributes(methodInfo, targetExpr, qualifier, argExprs, methodInfo.GetParameters(), attributedExprs);
         }
     }
     RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Left, true, false, null);
     RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Right, true, false, null);
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     if ((!analysis.ForWrites || isWritten) && (analysis.ForWrites || isRead))
     {
         StringBuilder builder = new StringBuilder("this/");
         for (RulePathQualifier qualifier2 = qualifier; qualifier2 != null; qualifier2 = qualifier2.Next)
         {
             builder.Append(qualifier2.Name);
             if (qualifier2.Name == "*")
             {
                 if (qualifier2.Next != null)
                 {
                     throw new NotSupportedException(Messages.InvalidWildCardInPathQualifier);
                 }
             }
             else
             {
                 builder.Append("/");
             }
         }
         analysis.AddSymbol(builder.ToString());
     }
 }
        private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack <MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions)
        {
            // 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);

            for (int m = 0; m < members.Length; ++m)
            {
                MemberInfo mi = members[m];
                if (!methodStack.Contains(mi)) // Prevent recursion
                {
                    methodStack.Push(mi);

                    object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true);
                    if (attrs != null && attrs.Length != 0)
                    {
                        RuleAttribute[] ruleAttrs = (RuleAttribute[])attrs;
                        for (int i = 0; i < ruleAttrs.Length; ++i)
                        {
                            RuleAttribute ruleAttr = ruleAttrs[i];

                            if (ruleAttr is RuleReadWriteAttribute readWriteAttr)
                            {
                                // Just analyze the read/write attribute normally.
                                readWriteAttr.Analyze(analysis, mi, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                            }
                            else
                            {
                                RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)ruleAttr;
                                invokeAttr.AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                            }
                        }
                    }

                    methodStack.Pop();
                }
            }
        }
        internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions)
        {
            // A RuleWrite attribute is only applicable if we're analyzing for writes.
            if (!analysis.ForWrites)
            {
                return;
            }

            base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
        }
        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 void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeObjectCreateExpression createExpression = (CodeObjectCreateExpression)expression;

            // check each parameter
            foreach (CodeExpression p in createExpression.Parameters)
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, p, true, false, null);
            }
        }
        internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
        {
            // A RuleWrite attribute is only applicable if we're analyzing for writes.
            if (!analysis.ForWrites)
                return;

            base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeBinaryOperatorExpression expression2 = (CodeBinaryOperatorExpression)expression;
            RuleBinaryExpressionInfo     info        = analysis.Validation.ExpressionInfo(expression2) as RuleBinaryExpressionInfo;

            if (info != null)
            {
                MethodInfo methodInfo = info.MethodInfo;
                if (methodInfo != null)
                {
                    List <CodeExpression>    attributedExprs = new List <CodeExpression>();
                    CodeExpressionCollection argExprs        = new CodeExpressionCollection();
                    argExprs.Add(expression2.Left);
                    argExprs.Add(expression2.Right);
                    CodeExpression targetExpr = new CodeTypeReferenceExpression(methodInfo.DeclaringType);
                    analysis.AnalyzeRuleAttributes(methodInfo, targetExpr, qualifier, argExprs, methodInfo.GetParameters(), attributedExprs);
                }
            }
            RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Left, true, false, null);
            RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Right, true, false, null);
        }
Exemple #22
0
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeMethodInvokeExpression expression2  = (CodeMethodInvokeExpression)expression;
            CodeExpression             targetObject = expression2.Method.TargetObject;

            if (analysis.Validation.ExpressionInfo(targetObject) == null)
            {
                InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception.Data["ErrorObject"] = targetObject;
                throw exception;
            }
            RuleMethodInvokeExpressionInfo info2 = analysis.Validation.ExpressionInfo(expression2) as RuleMethodInvokeExpressionInfo;

            if (info2 == null)
            {
                InvalidOperationException exception2 = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            MethodInfo            methodInfo      = info2.MethodInfo;
            List <CodeExpression> attributedExprs = new List <CodeExpression>();

            analysis.AnalyzeRuleAttributes(methodInfo, targetObject, qualifier, expression2.Parameters, methodInfo.GetParameters(), attributedExprs);
            if (!attributedExprs.Contains(targetObject))
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, true, false, null);
            }
            for (int i = 0; i < expression2.Parameters.Count; i++)
            {
                CodeExpression item = expression2.Parameters[i];
                if (!attributedExprs.Contains(item))
                {
                    RuleExpressionWalker.AnalyzeUsage(analysis, item, true, false, null);
                }
            }
        }
 internal abstract void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier);
 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);
                 }
             }
         }
     }
 }
Exemple #25
0
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodePropertyReferenceExpression expression2 = (CodePropertyReferenceExpression)expression;
            CodeExpression targetObject = expression2.TargetObject;

            if (analysis.Validation.ExpressionInfo(targetObject) == null)
            {
                InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception.Data["ErrorObject"] = targetObject;
                throw exception;
            }
            RulePropertyExpressionInfo info2 = analysis.Validation.ExpressionInfo(expression2) as RulePropertyExpressionInfo;

            if (info2 == null)
            {
                InvalidOperationException exception2 = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            PropertyInfo          propertyInfo    = info2.PropertyInfo;
            List <CodeExpression> attributedExprs = new List <CodeExpression>();

            analysis.AnalyzeRuleAttributes(propertyInfo, targetObject, qualifier, null, null, attributedExprs);
            if (!attributedExprs.Contains(targetObject))
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, new RulePathQualifier(propertyInfo.Name, qualifier));
            }
        }
Exemple #26
0
 private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack <MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions)
 {
     foreach (MemberInfo info in contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
     {
         if (!methodStack.Contains(info))
         {
             methodStack.Push(info);
             object[] customAttributes = info.GetCustomAttributes(typeof(RuleAttribute), true);
             if ((customAttributes != null) && (customAttributes.Length != 0))
             {
                 foreach (RuleAttribute attribute in (RuleAttribute[])customAttributes)
                 {
                     RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute;
                     if (attribute2 != null)
                     {
                         attribute2.Analyze(analysis, info, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                     }
                     else
                     {
                         ((RuleInvokeAttribute)attribute).AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                     }
                 }
             }
             methodStack.Pop();
         }
     }
 }
        private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack<MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
        {
            // 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);

            for (int m = 0; m < members.Length; ++m)
            {
                MemberInfo mi = members[m];
                if (!methodStack.Contains(mi)) // Prevent recursion
                {
                    methodStack.Push(mi);

                    object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true);
                    if (attrs != null && attrs.Length != 0)
                    {
                        RuleAttribute[] ruleAttrs = (RuleAttribute[])attrs;
                        for (int i = 0; i < ruleAttrs.Length; ++i)
                        {
                            RuleAttribute ruleAttr = ruleAttrs[i];

                            RuleReadWriteAttribute readWriteAttr = ruleAttr as RuleReadWriteAttribute;
                            if (readWriteAttr != null)
                            {
                                // Just analyze the read/write attribute normally.
                                readWriteAttr.Analyze(analysis, mi, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                            }
                            else
                            {
                                RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)ruleAttr;
                                invokeAttr.AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
                            }
                        }
                    }

                    methodStack.Pop();
                }
            }
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            // Analyze the target object, flowing down the qualifier from above.  An expression
            // like:
            //      this.a.b[2,3].c[4].d[5] = 99;
            // should produce a path similar to:
            //      this/a/b/c/d

            CodeArrayIndexerExpression arrayIndexerExpr = (CodeArrayIndexerExpression)expression;
            RuleExpressionWalker.AnalyzeUsage(analysis, arrayIndexerExpr.TargetObject, isRead, isWritten, qualifier);

            // Analyze the indexer arguments.  They are read.
            for (int i = 0; i < arrayIndexerExpr.Indices.Count; ++i)
                RuleExpressionWalker.AnalyzeUsage(analysis, arrayIndexerExpr.Indices[i], true, false, null);
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            string message;

            CodeIndexerExpression indexerExpr = (CodeIndexerExpression)expression;

            // Evaluate the target object and get its type.
            CodeExpression targetObject = indexerExpr.TargetObject;
            RuleExpressionInfo targetExprInfo = analysis.Validation.ExpressionInfo(targetObject);
            if (targetExprInfo == null)  // Oops, someone forgot to validate.
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated);
                InvalidOperationException exception = new InvalidOperationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = targetObject;
                throw exception;
            }

            // Get the property info from the validator so we can look for [RuleRead] and [RuleWrite] attributes.
            RulePropertyExpressionInfo propExprInfo = analysis.Validation.ExpressionInfo(indexerExpr) as RulePropertyExpressionInfo;
            if (propExprInfo == null)  // Oops, someone forgot to validate.
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated);
                InvalidOperationException exception = new InvalidOperationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = indexerExpr;
                throw exception;
            }

            PropertyInfo pi = propExprInfo.PropertyInfo;

            // Look for RuleAttribute's on the invoked indexer property.
            List<CodeExpression> attributedExprs = new List<CodeExpression>();
            analysis.AnalyzeRuleAttributes(pi, targetObject, qualifier, indexerExpr.Indices, pi.GetIndexParameters(), attributedExprs);

            // See if the target object needs default analysis.
            if (!attributedExprs.Contains(targetObject))
            {
                // The property had no [RuleRead] or [RuleWrite] attributes.  The target object is read or
                // written (as the case may be).

                RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, qualifier);
            }

            // See if any of the arguments need default analysis.
            for (int i = 0; i < indexerExpr.Indices.Count; ++i)
            {
                CodeExpression argExpr = indexerExpr.Indices[i];

                if (!attributedExprs.Contains(argExpr))
                {
                    // Similar to the target object, we assume that this method can reads the value
                    // of the parameter, but none of its members.
                    RuleExpressionWalker.AnalyzeUsage(analysis, argExpr, true, false, null);
                }
            }
        }
        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 void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeArrayIndexerExpression expression2 = (CodeArrayIndexerExpression)expression;

            RuleExpressionWalker.AnalyzeUsage(analysis, expression2.TargetObject, isRead, isWritten, qualifier);
            for (int i = 0; i < expression2.Indices.Count; i++)
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Indices[i], true, false, null);
            }
        }
 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 AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeArrayCreateExpression createExpression = (CodeArrayCreateExpression)expression;

            if (createExpression.SizeExpression != null)
                RuleExpressionWalker.AnalyzeUsage(analysis, createExpression.SizeExpression, true, false, null);
            foreach (CodeExpression p in createExpression.Initializers)
                RuleExpressionWalker.AnalyzeUsage(analysis, p, true, false, null);
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeObjectCreateExpression expression2 = (CodeObjectCreateExpression)expression;

            foreach (CodeExpression expression3 in expression2.Parameters)
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);
            }
        }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeDirectionExpression expression2 = (CodeDirectionExpression)expression;
            CodeExpression          expression3 = expression2.Expression;
            bool flag  = false;
            bool flag2 = true;
            RulePathQualifier qualifier2 = null;

            switch (expression2.Direction)
            {
            case FieldDirection.In:
                flag       = false;
                flag2      = true;
                qualifier2 = new RulePathQualifier("*", null);
                break;

            case FieldDirection.Out:
                flag       = true;
                flag2      = false;
                qualifier2 = null;
                break;

            case FieldDirection.Ref:
                flag       = true;
                flag2      = true;
                qualifier2 = analysis.ForWrites ? null : new RulePathQualifier("*", null);
                break;
            }
            RuleExpressionWalker.AnalyzeUsage(analysis, expression3, flag2, flag, qualifier2);
        }
 public RulePathQualifier(string name, RulePathQualifier next)
 {
     this.name = name;
     this.next = next;
 }
 internal abstract void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions);
 internal void AnalyzeRuleAttributes(MemberInfo member, CodeExpression targetExpr, RulePathQualifier targetQualifier, CodeExpressionCollection argExprs, ParameterInfo[] parameters, List <CodeExpression> attributedExprs)
 {
     object[] attrs = member.GetCustomAttributes(typeof(RuleAttribute), true);
     if (attrs != null && attrs.Length > 0)
     {
         RuleAttribute[] ruleAttrs = (RuleAttribute[])attrs;
         for (int i = 0; i < ruleAttrs.Length; ++i)
         {
             ruleAttrs[i].Analyze(this, member, targetExpr, targetQualifier, argExprs, parameters, attributedExprs);
         }
     }
 }
        internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions)
        {
            Stack <MemberInfo> methodStack = new Stack <MemberInfo>();

            methodStack.Push(member);

            AnalyzeInvokeAttribute(analysis, member.DeclaringType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);

            methodStack.Pop();
        }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     if ((!analysis.ForWrites || isWritten) && (analysis.ForWrites || isRead))
     {
         StringBuilder builder = new StringBuilder("this/");
         for (RulePathQualifier qualifier2 = qualifier; qualifier2 != null; qualifier2 = qualifier2.Next)
         {
             builder.Append(qualifier2.Name);
             if (qualifier2.Name == "*")
             {
                 if (qualifier2.Next != null)
                 {
                     throw new NotSupportedException(Messages.InvalidWildCardInPathQualifier);
                 }
             }
             else
             {
                 builder.Append("/");
             }
         }
         analysis.AddSymbol(builder.ToString());
     }
 }
Exemple #41
0
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeArrayCreateExpression expression2 = (CodeArrayCreateExpression)expression;

            if (expression2.SizeExpression != null)
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, expression2.SizeExpression, true, false, null);
            }
            foreach (CodeExpression expression3 in expression2.Initializers)
            {
                RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);
            }
        }
 internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List <CodeExpression> attributedExpressions)
 {
     if (analysis.ForWrites)
     {
         base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
     }
 }
 public RulePathQualifier(string name, RulePathQualifier next)
 {
     this.name = name;
     this.next = next;
 }
 internal void AnalyzeRuleAttributes(MemberInfo member, CodeExpression targetExpr, RulePathQualifier targetQualifier, CodeExpressionCollection argExprs, ParameterInfo[] parameters, List <CodeExpression> attributedExprs)
 {
     object[] customAttributes = member.GetCustomAttributes(typeof(RuleAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         RuleAttribute[] attributeArray = (RuleAttribute[])customAttributes;
         for (int i = 0; i < attributeArray.Length; i++)
         {
             attributeArray[i].Analyze(this, member, targetExpr, targetQualifier, argExprs, parameters, attributedExprs);
         }
     }
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeCastExpression expression2 = (CodeCastExpression) expression;
     RuleExpressionWalker.AnalyzeUsage(analysis, expression2.Expression, true, false, null);
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     ruleExpr.AnalyzeUsage(analysis, isRead, isWritten, qualifier);
 }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     this.ruleExpr.AnalyzeUsage(analysis, isRead, isWritten, qualifier);
 }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeBinaryOperatorExpression binaryExpr = (CodeBinaryOperatorExpression)expression;

            // Get the method info from the validation so we can look for [RuleRead] and [RuleWrite] attributes.
            RuleBinaryExpressionInfo expressionInfo = analysis.Validation.ExpressionInfo(binaryExpr) as RuleBinaryExpressionInfo;
            if (expressionInfo != null)
            {
                // we may be calling a method, not a default operator
                MethodInfo method = expressionInfo.MethodInfo;
                if (method != null)
                {
                    List<CodeExpression> attributedExprs = new List<CodeExpression>();
                    CodeExpressionCollection arguments = new CodeExpressionCollection();
                    arguments.Add(binaryExpr.Left);
                    arguments.Add(binaryExpr.Right);
                    CodeExpression targetObject = new CodeTypeReferenceExpression(method.DeclaringType);
                    analysis.AnalyzeRuleAttributes(method, targetObject, qualifier, arguments, method.GetParameters(), attributedExprs);
                }
            }

            // Analyze the left & right children.
            RuleExpressionWalker.AnalyzeUsage(analysis, binaryExpr.Left, true, false, null);
            RuleExpressionWalker.AnalyzeUsage(analysis, binaryExpr.Right, true, false, null);
        }
Exemple #49
0
 public static void AnalyzeUsage(RuleAnalysis analysis, CodeExpression expression, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     if (analysis == null)
     {
         throw new ArgumentNullException("analysis");
     }
     GetExpression(expression).AnalyzeUsage(expression, analysis, isRead, isWritten, qualifier);
 }
        internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
        {
            CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression)expression;
            CodeExpression targetObject = expression2.TargetObject;

            RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, new RulePathQualifier(expression2.FieldName, qualifier));
        }
 internal override void AnalyzeUsage(CodeExpression expression, RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)
 {
     CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression) expression;
     CodeExpression targetObject = expression2.TargetObject;
     RuleExpressionWalker.AnalyzeUsage(analysis, targetObject, isRead, isWritten, new RulePathQualifier(expression2.FieldName, qualifier));
 }
 internal abstract void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions);