当前位置: 首页>>代码示例>>C#>>正文


C# CsElement.FindParentElement方法代码示例

本文整理汇总了C#中CsElement.FindParentElement方法的典型用法代码示例。如果您正苦于以下问题:C# CsElement.FindParentElement方法的具体用法?C# CsElement.FindParentElement怎么用?C# CsElement.FindParentElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CsElement的用法示例。


在下文中一共展示了CsElement.FindParentElement方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateCommentForCreatedByElement

 private void ValidateCommentForCreatedByElement(string commentLine, CsElement parentElement)
 {
     if (commentLine.Contains("CreatedBy"))
     {
         if (!commentLine.Contains("Chirag Lad."))
         {
             try
             {
                 this.AddViolation(parentElement.FindParentElement(), "CreatedByRule", "Should only be created by Chirag Lad.");
             }
             catch (Exception ex)
             {
             }
         }
     }
 }
开发者ID:iChiragLad,项目名称:stylecop-CustomRule,代码行数:16,代码来源:NamingConventions.cs

示例2: ProcessElement

        /// <summary>
        /// Checks one element and its children.
        /// </summary>
        /// <param name="document">
        /// The document object representing the code file.
        /// </param>
        /// <param name="element">
        /// The element to check.
        /// </param>
        /// <returns>
        /// Returns false if the analyzer should quit.
        /// </returns>
        private bool ProcessElement(CsDocument document, CsElement element)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(element, "element");

            if (!this.Cancel)
            {
                if (!element.Generated)
                {
                    if (element.ElementType == ElementType.Method || element.ElementType == ElementType.Constructor)
                    {
                        if (!element.Declaration.ContainsModifier(CsTokenType.Abstract))
                        {
                            // Check the parameter verification.
                            this.CheckMethodParameterVerification(element);
                        }
                    }
                    else if (element.ElementType == ElementType.Accessor)
                    {
                        CsElement parent = element.FindParentElement();

                        if (parent != null && !parent.Declaration.ContainsModifier(CsTokenType.Abstract))
                        {
                            // Check the parameter verification.
                            if (parent.ElementType == ElementType.Indexer)
                            {
                                if (element.Declaration.Tokens.First.Value.Text == "set")
                                {
                                    this.CheckMethodParameterVerification(element);
                                }
                            }
                            else if (parent.ElementType == ElementType.Property)
                            {
                                if (element.Declaration.Tokens.First.Value.Text == "set")
                                {
                                    this.CheckPropertyParameterVerification(element);
                                }
                            }
                        }
                    }
                }

                // Do not check the methods and properties within interfaces.
                if (element.ElementType != ElementType.Interface)
                {
                    foreach (CsElement child in element.ChildElements)
                    {
                        if (!this.ProcessElement(document, child))
                        {
                            return false;
                        }
                    }
                }

                return true;
            }

            return false;
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:71,代码来源:ParamCheck.cs

示例3: CheckPropertyParameterVerification

        /// <summary>
        /// Checks that set properties verify the input value.
        /// </summary>
        /// <param name="element">
        /// The property to check.
        /// </param>
        private void CheckPropertyParameterVerification(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            // Determine whether the accessor has a body. If this is an automatic property,
            // there will be no body.
            bool hasBody = false;
            foreach (CsToken token in element.Tokens)
            {
                if (token.CsTokenType == CsTokenType.OpenCurlyBracket)
                {
                    hasBody = true;
                    break;
                }
            }

            if (hasBody)
            {
                // Get the list of param check words.
                List<ParamTokens> paramCheckTokens = GetParamCheckTokens(element);

                // Make sure the list contains "value".
                Node<CsToken> paramTokenNode = null;
                foreach (ParamTokens paramTokens in paramCheckTokens)
                {
                    foreach (Node<CsToken> tokenNode in paramTokens.TokenNodes)
                    {
                        if (tokenNode.Value.Text == "value")
                        {
                            paramTokenNode = paramTokens.ParamTokenNode;
                            break;
                        }
                    }
                }

                CsElement parent = element.FindParentElement();

                if (paramTokenNode == null)
                {
                    if (element.Declaration.Name != "Ignore" || (parent != null && parent.Declaration.Name != "Param"))
                    {
                        this.AddViolation(element, Rules.ParametersMustBeVerified, "value");
                    }
                }
                else
                {
                    if (parent != null
                        && (parent.ActualAccess == AccessModifierType.Private || parent.ActualAccess == AccessModifierType.Internal
                            || parent.ActualAccess == AccessModifierType.ProtectedInternal))
                    {
                        if (paramTokenNode.Value.Text.StartsWith("Param.Require", StringComparison.Ordinal))
                        {
                            Rules type = Rules.PrivateMethodsMustUseAsserts;

                            /*if (this.autoUpdate)
                            {
                                if (this.ChangeStatement(document, paramToken, true))
                                {
                                    type = ViolationID.ParamCheckPrivateRequireWarning;
                                }
                            }*/
                            this.AddViolation(element, paramTokenNode.Value.LineNumber, type);
                        }
                    }
                    else
                    {
                        if (paramTokenNode.Value.Text.StartsWith("Param.Assert", StringComparison.Ordinal))
                        {
                            Rules type = Rules.PublicMethodsMustUseRequires;

                            /*if (this.autoUpdate)
                            {
                                if (this.ChangeStatement(document, paramToken, false))
                                {
                                    type = ViolationID.ParamCheckPublicAssert;
                                }
                            }*/
                            this.AddViolation(element, paramTokenNode.Value.LineNumber, type);
                        }
                    }
                }
            }
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:89,代码来源:ParamCheck.cs

示例4: CheckMethodParameterVerification

        /// <summary>
        /// Checks that all method parameters are verified.
        /// </summary>
        /// <param name="element">
        /// The method to check.
        /// </param>
        private void CheckMethodParameterVerification(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            IParameterContainer parameterContainer = element as IParameterContainer;
            Debug.Assert(parameterContainer != null, "The element does not contain parameters.");

            // If there are no parameters, don't do anything.
            if (parameterContainer.Parameters.Count > 0)
            {
                // Get the list of param check tokens.
                List<ParamTokens> paramCheckTokens = GetParamCheckTokens(element);

                // Find each parameter.
                foreach (Parameter parameter in parameterContainer.Parameters)
                {
                    Node<CsToken> paramTokenNode = null;
                    foreach (ParamTokens paramTokens in paramCheckTokens)
                    {
                        bool found = false;

                        foreach (Node<CsToken> tokenNode in paramTokens.TokenNodes)
                        {
                            if (parameter.Name == tokenNode.Value.Text)
                            {
                                paramTokenNode = paramTokens.ParamTokenNode;
                                found = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            break;
                        }
                    }

                    if (paramTokenNode == null)
                    {
                        CsElement parent = element.FindParentElement();

                        if ((parameter.Modifiers & ParameterModifiers.Out) == 0
                            && (element.Declaration.Name != "Ignore" || (parent != null && parent.Declaration.Name != "Param")))
                        {
                            this.AddViolation(element, Rules.ParametersMustBeVerified, parameter.Name);
                        }
                    }
                    else
                    {
                        if ((parameter.Modifiers & ParameterModifiers.Out) != 0)
                        {
                            this.AddViolation(element, paramTokenNode.Value.LineNumber, Rules.OutParametersMustNotBeVerified, parameter.Name);
                        }
                        else
                        {
                            // Get the param statement type.
                            if (paramTokenNode != null && paramTokenNode.Next != null)
                            {
                                Node<CsToken> paramCheckTypeNode = paramTokenNode.Next.Next;
                                if (paramCheckTypeNode != null && paramCheckTypeNode.Value.CsTokenType == CsTokenType.Other)
                                {
                                    if (element.ActualAccess == AccessModifierType.Private || element.ActualAccess == AccessModifierType.Internal
                                        || element.ActualAccess == AccessModifierType.ProtectedInternal)
                                    {
                                        if (paramCheckTypeNode.Value.Text.StartsWith("Require", StringComparison.Ordinal))
                                        {
                                            Rules type = Rules.PrivateMethodsMustUseAsserts;

                                            /*if (this.autoUpdate)
                                            {
                                                if (this.ChangeStatement(document, paramToken, true))
                                                {
                                                    type = ViolationID.ParamCheckPrivateRequireWarning;
                                                }
                                            }*/
                                            this.AddViolation(element, paramTokenNode.Value.LineNumber, type);
                                        }
                                    }
                                    else
                                    {
                                        if (paramCheckTypeNode.Value.Text.StartsWith("Assert", StringComparison.Ordinal))
                                        {
                                            Rules type = Rules.PublicMethodsMustUseRequires;

                                            /*if (this.autoUpdate)
                                            {
                                                if (this.ChangeStatement(document, paramToken, false))
                                                {
                                                    type = ViolationID.ParamCheckPublicAssertWarning;
                                                }
                                            }*/
                                            this.AddViolation(element, paramTokenNode.Value.LineNumber, type);
                                        }
                                    }
//.........这里部分代码省略.........
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:101,代码来源:ParamCheck.cs

示例5: CheckFieldAccessModifiers

        /// <summary>
        /// Verifies that fields are not declared public.
        /// </summary>
        /// <param name="element">
        /// The element to check.
        /// </param>
        private void CheckFieldAccessModifiers(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            CsElement parent = element.FindParentElement();

            if (element.ElementType == ElementType.Field && (element.Declaration.AccessModifierType != AccessModifierType.Private) && parent != null
                && parent.ElementType != ElementType.Struct)
            {
                // If the field is located within a native methods class, and the class that contains
                // the field is private or internal, then do not check the access modifiers on the field.
                bool nativeMethods = false;
                bool privateOrInternal = false;
                while (parent != null)
                {
                    if (parent.ElementType != ElementType.Class && parent.ElementType != ElementType.Struct)
                    {
                        break;
                    }

                    if (parent.ActualAccess == AccessModifierType.Private || parent.ActualAccess == AccessModifierType.Internal)
                    {
                        privateOrInternal = true;
                    }

                    if (parent.Declaration.Name.EndsWith("NativeMethods", StringComparison.Ordinal))
                    {
                        nativeMethods = true;
                        break;
                    }

                    parent = parent.FindParentElement();
                }

                if (!nativeMethods || !privateOrInternal)
                {
                    Field field = (Field)element;
                    if (!field.Const && !field.Readonly && !field.Generated)
                    {
                        this.AddViolation(element, Rules.FieldsMustBePrivate);
                    }
                }
            }
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:50,代码来源:MaintainabilityRules.cs

示例6: CheckAccessModifierRulesForElement

        /// <summary>
        /// Checks the access modifier on the element.
        /// </summary>
        /// <param name="element">
        /// The element being visited.
        /// </param>
        private void CheckAccessModifierRulesForElement(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            // Make sure this element is not generated.
            if (!element.Generated)
            {
                // Skip these rules if the element is a child of an interface.
                CsElement parent = element.FindParentElement();
                if (parent == null || parent.ElementType != ElementType.Interface)
                {
                    this.CheckForAccessModifier(element);
                    this.CheckFieldAccessModifiers(element);
                }
            }
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:22,代码来源:MaintainabilityRules.cs

示例7: CheckUsingDirectivePlacement

        /// <summary>
        /// Checks that using-directives are placed within the namespace element.
        /// </summary>
        /// <param name="element">
        /// The element to check.
        /// </param>
        private void CheckUsingDirectivePlacement(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            // Only check the positioning of using directives which are not within generated code.
            if (!element.Generated && element.ElementType == ElementType.UsingDirective)
            {
                CsElement parentElement = element.FindParentElement();
                if (parentElement != null && parentElement.ElementType != ElementType.Namespace)
                {
                    // This is acceptable if there is no namespace in the file at all.
                    bool foundNamespace = false;
                    if (parentElement.ElementType == ElementType.Root)
                    {
                        foreach (CsElement child in parentElement.ChildElements)
                        {
                            if (child.ElementType == ElementType.Namespace)
                            {
                                foundNamespace = true;
                                break;
                            }
                        }
                    }

                    // Its also acceptable if there is an assembly attribute too.
                    bool foundAssemblyAttribute = false;
                    if (parentElement.ElementType == ElementType.Root)
                    {
                        foreach (CsElement child in parentElement.ChildElements)
                        {
                            if (child.ElementType == ElementType.AssemblyOrModuleAttribute)
                            {
                                foundAssemblyAttribute = true;
                                break;
                            }
                        }
                    }

                    if (foundNamespace && !foundAssemblyAttribute)
                    {
                        this.AddViolation(element, Rules.UsingDirectivesMustBePlacedWithinNamespace);
                    }
                }
            }
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:51,代码来源:OrderingRules.cs

示例8: CheckSiblingAccessors

        /// <summary>
        /// Checks an accessor and its siblings. If the access is all on one line, its siblings
        /// must also be all on one line.
        /// </summary>
        /// <param name="accessor">The accessor to check.</param>
        /// <param name="openingBracketNode">The opening bracket within the accessor.</param>
        private void CheckSiblingAccessors(CsElement accessor, Node<CsToken> openingBracketNode)
        {
            Param.AssertNotNull(accessor, "accessor");
            Param.AssertNotNull(openingBracketNode, "openingBracketNode");

            Bracket openingBracket = (Bracket)openingBracketNode.Value;

            if (openingBracket.MatchingBracket != null &&
                openingBracket.LineNumber == openingBracket.MatchingBracket.LineNumber)
            {
                // Check the siblings of this accessor.
                CsElement property = accessor.FindParentElement();
                if (property != null)
                {
                    foreach (CsElement sibling in property.ChildElements)
                    {
                        if (sibling != accessor)
                        {
                            // Check this sibling to make sure it is all on one line.
                            Node<CsToken> openBracketNode = LayoutRules.GetOpenBracket(sibling.Tokens);
                            if (openBracketNode != null)
                            {
                                openingBracket = (Bracket)openBracketNode.Value;
                                if (openingBracket != null && openingBracket.MatchingBracket != null)
                                {
                                    if (openingBracket.LineNumber != openingBracket.MatchingBracket.LineNumber)
                                    {
                                        this.AddViolation(
                                            accessor,
                                            accessor.LineNumber,
                                            Rules.AllAccessorsMustBeMultiLineOrSingleLine,
                                            property.FriendlyTypeText);

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
开发者ID:katerina-marchenkova,项目名称:my,代码行数:48,代码来源:LayoutRules.cs

示例9: CheckElementCurlyBracketPlacement

        /// <summary>
        /// Checks the curly brackets under the given element.
        /// </summary>
        /// <param name="element">
        /// The element being visited.
        /// </param>
        private void CheckElementCurlyBracketPlacement(CsElement element)
        {
            Param.AssertNotNull(element, "element");

            if (!element.Generated)
            {
                // Check the type of the element.
                if (element.ElementType == ElementType.Accessor)
                {
                    // Curly brackets are allowed to be all on one line for accessors.
                    this.CheckElementBracketPlacement(element, true);
                }
                else if (element.ElementType == ElementType.Class || element.ElementType == ElementType.Constructor || element.ElementType == ElementType.Destructor
                         || element.ElementType == ElementType.Enum || element.ElementType == ElementType.Event || element.ElementType == ElementType.Indexer
                         || element.ElementType == ElementType.Interface || element.ElementType == ElementType.Method || element.ElementType == ElementType.Namespace
                         || element.ElementType == ElementType.Struct)
                {
                    bool allowOnOneLine = false;

                    // Curly brackets for these types cannot be all on one line.
                    if (element.ElementType == ElementType.Indexer)
                    {
                        CsElement parentElement = element.FindParentElement();

                        // If its an indexer on an interface then it can be on one line.
                        // An indexer on an abstract class is also ok.
                        if (parentElement != null)
                        {
                            if (parentElement.ElementType == ElementType.Interface
                                || (parentElement.ElementType == ElementType.Class && parentElement.Declaration.ContainsModifier(CsTokenType.Abstract)))
                            {
                                allowOnOneLine = true;
                            }
                        }
                    }

                    this.CheckElementBracketPlacement(element, allowOnOneLine);
                }
                else if (element.ElementType == ElementType.Property)
                {
                    // Automatic properties are allowed to be on a single line, but normal properties are not.
                    this.CheckElementBracketPlacement(element, IsAutomaticProperty((Property)element));
                }
            }
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:51,代码来源:LayoutRules.cs


注:本文中的CsElement.FindParentElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。