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


C# CsElement类代码示例

本文整理汇总了C#中CsElement的典型用法代码示例。如果您正苦于以下问题:C# CsElement类的具体用法?C# CsElement怎么用?C# CsElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: GetCodeIssues

        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            CodePoint startPoint = null;
            CodePoint endPoint = null;
            foreach (var token in from token in csElement.ElementTokens
                                  where token.LineNumber >= violation.Line
                                  select token)
            {
                if (token.CsTokenType == CsTokenType.UsingDirective || token.CsTokenType == CsTokenType.Using)
                {
                    startPoint = token.Location.StartPoint;
                    continue;
                }

                if (token.CsTokenType == CsTokenType.Semicolon)
                {
                    endPoint = token.Location.EndPoint;
                }

                if (startPoint != null && endPoint != null)
                {
                    var sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, endPoint.LineNumber, endPoint.IndexOnLine + 2);
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                }
            }
        }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:30,代码来源:UsingDirectiveCodeIssue.cs

示例2: GetCodeIssues

        public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
            ISourceCode sourceCode,
            Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
            Violation violation,
            CsElement csElement)
        {
            var preprocessorToken = (from token in csElement.ElementTokens
                         where token.LineNumber == violation.Line && token.CsTokenType == CsTokenType.PreprocessorDirective
                         select token).FirstOrDefault();
            if (preprocessorToken != null)
            {
                int underlineLength = 1;
                while (preprocessorToken.Text[underlineLength] == ' ' || preprocessorToken.Text[underlineLength] == '\t')
                {
                    underlineLength++;
                }

                while (underlineLength < preprocessorToken.Text.Length && preprocessorToken.Text[underlineLength] != ' ' && preprocessorToken.Text[underlineLength] != '\t')
                {
                    underlineLength++;
                }

                var startPoint = preprocessorToken.Location.StartPoint;
                var sourceRange = new SourceRange(startPoint.LineNumber, startPoint.IndexOnLine + 1, startPoint.LineNumber, startPoint.IndexOnLine + 1 + underlineLength);
                yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
            }
        }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:27,代码来源:PreprocessorDirectiveIssueLocator.cs

示例3: Property

        /// <summary>
        /// Initializes a new instance of the Property class.
        /// </summary>
        /// <param name="document">
        /// The document that contains the element.
        /// </param>
        /// <param name="parent">
        /// The parent of the element.
        /// </param>
        /// <param name="header">
        /// The Xml header for this element.
        /// </param>
        /// <param name="attributes">
        /// The list of attributes attached to this element.
        /// </param>
        /// <param name="declaration">
        /// The declaration code for this element.
        /// </param>
        /// <param name="returnType">
        /// The property return type.
        /// </param>
        /// <param name="unsafeCode">
        /// Indicates whether the element resides within a block of unsafe code.
        /// </param>
        /// <param name="generated">
        /// Indicates whether the code element was generated or written by hand.
        /// </param>
        internal Property(
            CsDocument document, 
            CsElement parent, 
            XmlHeader header, 
            ICollection<Attribute> attributes, 
            Declaration declaration, 
            TypeToken returnType, 
            bool unsafeCode, 
            bool generated)
            : base(document, parent, ElementType.Property, "property " + declaration.Name, header, attributes, declaration, unsafeCode, generated)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(header);
            Param.Ignore(attributes);
            Param.AssertNotNull(declaration, "declaration");
            Param.AssertNotNull(returnType, "returnType");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);

            this.returnType = returnType;

            // If this is an explicit interface member implementation and our access modifier
            // is currently set to private because we don't have one, then it should be public instead.
            if (this.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1 && !this.Declaration.Name.StartsWith("this.", StringComparison.Ordinal))
            {
                this.Declaration.AccessModifierType = AccessModifierType.Public;
            }
        }
开发者ID:RainsSoft,项目名称:StyleCop,代码行数:56,代码来源:Property.cs

示例4: CheckClasses

        /// <summary>
        /// Checks whether specified element conforms to custom rule GP0001
        /// </summary>
        /// <param name="element">
        /// The current element that is to be inspected.
        /// </param>
        /// <param name="parentElement">
        /// The parent element for the element currently being inspected.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// True/False indicating whether to continue walking the elements within the class.
        /// </returns>
        private bool CheckClasses(CsElement element, CsElement parentElement, object context)
        {
            // If the overall StyleCop is cancelled externally, then stop processing
            if (Cancel)
            {
                return false;
            }

            // if current element is not a class then continue walking
            if (element.ElementType != ElementType.Class)
            {
                return true;
            }

            // check whether class name contains "a" letter
            var classElement = (Class)element;
            if (classElement.Declaration.Name.Contains("a"))
            {
                // add violation
                // (note how custom message arguments could be used)
                AddViolation(
                    classElement, classElement.Location, "AvoidUsingAInClassNames", classElement.FriendlyTypeText);
            }

            if (classElement.Declaration.Name.Length > MaximumClassNameLength)
            {
                // add violation
                // (note how custom message arguments could be used)
                AddViolation(classElement, classElement.Location, "AvoidLongClassNames", classElement.FriendlyTypeText);
            }

            // continue walking in order to find all classes in file
            return true;
        }
开发者ID:gep13,项目名称:StyleCopDemos,代码行数:49,代码来源:MyCustomAnalyzer.cs

示例5: VisitElement

 public override void VisitElement(CsElement element, CsElement parentElement, object context)
 {
     if ((element.ElementType == ElementType.Method && ((Method)element).ReturnType.Text != "void") // Func
         || (element.ElementType == ElementType.Accessor && ((Accessor)element).AccessorType == AccessorType.Get)) // Getter
     {
         if (element.ElementTokens.Count(x => x.CsTokenType == CsTokenType.Return) > 1)
         {
             this.SourceAnalyzer.AddViolation(element, ContribRule.SingleReturnStatement);
         }
     }
     else if (element.ElementType == ElementType.Constructor
         || (element.ElementType == ElementType.Method && ((Method)element).ReturnType.Text == "void") // Proc
         || (element.ElementType == ElementType.Accessor && ((Accessor)element).AccessorType != AccessorType.Get)) // Setter, Add, Remove
     {
         if (element.ElementTokens.Any(x => x.CsTokenType == CsTokenType.Return))
         {
             this.SourceAnalyzer.AddViolation(element, ContribRule.ReturnStatementOnlyInFunctions);
         }
     }
     else if (element.ElementType == ElementType.Class)
     {
         if (element.Declaration.Name.Length > MaximumClassNameLength)
         {
             this.SourceAnalyzer.AddViolation(element, ContribRule.ClassNameLengthExceeded);
         }
     }
 }
开发者ID:gep13,项目名称:StyleCopDemos,代码行数:27,代码来源:MaintainabilityAnalyzer.cs

示例6: GetCodeIssues

 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     bool specialPredecessorFound = true;
     bool whitespaceFound = false;
     foreach (var token in this.getTokens(csElement).Flatten().Where(x => x.LineNumber == violation.Line))
     {
         if (!specialPredecessorFound && whitespaceFound && this.reportIssueFor(token, violation))
         {
             whitespaceFound = false;
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(token.Location.StartPoint.LineNumber, token.Location.StartPoint.IndexOnLine + 1, token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2));
         }
         else if (this.specialPredecessors.Contains(token.CsTokenType))
         {
             specialPredecessorFound = true;
             whitespaceFound = false;
         }
         else if (token.CsTokenType == CsTokenType.WhiteSpace)
         {
             whitespaceFound = true;
         }
         else
         {
             specialPredecessorFound = false;
             whitespaceFound = false;
         }
     }
 }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:31,代码来源:AllTokensPrecededByBannedWhitespaceNotPrecededBySpecialElementIssueLocator.cs

示例7: elementCallback

        private bool elementCallback(CsElement element, CsElement parentElement, object context)
        {
            if (element.Header != null)
            {
                XDocument xml;
                try
                {
                    var xmlText = string.Format("<root>{0}</root>", element.Header.RawText);
                    xml = XDocument.Parse(xmlText, LoadOptions.PreserveWhitespace); // Header.Text / Header.RawText の違いを気にしていない
                }
                catch
                {
                    return true; // XMLコメントの形式ミスはこのルールで検出しない
                }

                foreach (var xpathRule in rules)
                {
                    if (xml.XPathEvaluate<XObject>(xpathRule.XPath).Any())
                    {
                        this.Violate(element, element.Header.Location, xpathRule.Message);
                    }
                }
            }

            return true;
        }
开发者ID:lavn0,项目名称:CodeAnalysis,代码行数:26,代码来源:XmlCommentRule.cs

示例8: Destructor

        /// <summary>
        /// Initializes a new instance of the Destructor class.
        /// </summary>
        /// <param name="document">The documenent that contains the element.</param>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="header">The Xml header for this element.</param>
        /// <param name="attributes">The list of attributes attached to this element.</param>
        /// <param name="declaration">The declaration code for this element.</param>
        /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param>
        /// <param name="generated">Indicates whether the code element was generated or written by hand.</param>
        internal Destructor(
            CsDocument document,
            CsElement parent,
            XmlHeader header,
            ICollection<Attribute> attributes,
            Declaration declaration,
            bool unsafeCode,
            bool generated)
            : base(document,
            parent,
            ElementType.Destructor,
            "destructor " + declaration.Name,
            header,
            attributes,
            declaration,
            unsafeCode,
            generated)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(header);
            Param.Ignore(attributes);
            Param.AssertNotNull(declaration, "declaration");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);

            // Static destructors are always public.
            if (this.Declaration.ContainsModifier(CsTokenType.Static))
            {
                this.Declaration.AccessModifierType = AccessModifierType.Public;
            }
        }
开发者ID:katerina-marchenkova,项目名称:my,代码行数:42,代码来源:Destructor.cs

示例9: GetCodeIssues

 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (BinaryOperatorExpression operation in from x in enumerate(new ElementTypeFilter(LanguageElementType.BinaryOperatorExpression))
                                                    let binaryOperation = (BinaryOperatorExpression)x.ToLanguageElement()
                                                    where binaryOperation.RecoveredRange.Start.Line == violation.Line
                                                       && (binaryOperation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression
                                                           || binaryOperation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression)
                                                    select binaryOperation)
     {
         var parentType = operators[operation.Name];
         var leftChildType = operation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operators[operation.LeftSide.Name] : parentType;
         var rightChildType = operation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operators[operation.RightSide.Name] : parentType;
         var isLeftModulo = operation.LeftSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operation.LeftSide.Name == "%" : operation.Name == "%";
         var isRightModulo = operation.RightSide.ElementType == LanguageElementType.BinaryOperatorExpression ? operation.RightSide.Name == "%" : operation.Name == "%";
         if (rightChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.RightSide.RecoveredRange);
         }
         else if (leftChildType > parentType || (leftChildType == rightChildType && isLeftModulo != isRightModulo))
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.LeftSide.RecoveredRange);
         }
     }
 }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:28,代码来源:SA1407_ArithmeticExpressionsMustDeclarePrecedence.cs

示例10: GetCodeIssues

 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode, 
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
     Violation violation, 
     CsElement csElement)
 {
     bool predecessorFound = true;
     CodeLocation issueLocation = null;
     foreach (var token in this.getTokens(csElement).Flatten().Where(x => x.LineNumber == violation.Line))
     {
         if (!predecessorFound && !requiredFollowers.Contains(token.CsTokenType) && issueLocation != null)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, new SourceRange(issueLocation.StartPoint.LineNumber, issueLocation.StartPoint.IndexOnLine + 1, issueLocation.EndPoint.LineNumber, issueLocation.EndPoint.IndexOnLine + 2));
             predecessorFound = false;
             issueLocation = null;
         }
         else if (reportIssueFor(token, violation))
         {
             issueLocation = token.Location;
         }
         else if (requiredPredecessors.Contains(token.CsTokenType))
         {
             predecessorFound = true;
         }
         else
         {
             predecessorFound = false;
             issueLocation = null;
         }
     }
 }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:31,代码来源:SA1025_CodeMustNotContainMultipleWhitespaceInARow.cs

示例11: Accessor

        /// <summary>
        /// Initializes a new instance of the Accessor class.
        /// </summary>
        /// <param name="document">
        /// The document that contains the element.
        /// </param>
        /// <param name="parent">
        /// The parent of the element.
        /// </param>
        /// <param name="accessorType">
        /// The type of the accessor.
        /// </param>
        /// <param name="header">
        /// The Xml header for this element.
        /// </param>
        /// <param name="attributes">
        /// The list of attributes attached to this element.
        /// </param>
        /// <param name="declaration">
        /// The declaration code for this element.
        /// </param>
        /// <param name="unsafeCode">
        /// Indicates whether the element resides within a block of unsafe code.
        /// </param>
        /// <param name="generated">
        /// Indicates whether the code element was generated or written by hand.
        /// </param>
        internal Accessor(
            CsDocument document, 
            CsElement parent, 
            AccessorType accessorType, 
            XmlHeader header, 
            ICollection<Attribute> attributes, 
            Declaration declaration, 
            bool unsafeCode, 
            bool generated)
            : base(document, parent, ElementType.Accessor, declaration.Name + " accessor", header, attributes, declaration, unsafeCode, generated)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(accessorType);
            Param.Ignore(header);
            Param.Ignore(attributes);
            Param.AssertNotNull(declaration, "declaration");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);

            this.accessorType = accessorType;

            // Make sure the type and name match.
            Debug.Assert(
                (accessorType == AccessorType.Get && declaration.Name == "get") || (accessorType == AccessorType.Set && declaration.Name == "set")
                || (accessorType == AccessorType.Add && declaration.Name == "add") || (accessorType == AccessorType.Remove && declaration.Name == "remove"), 
                "The accessor type does not match its name.");

            this.FillDetails(parent);
        }
开发者ID:kopelli,项目名称:Visual-StyleCop,代码行数:57,代码来源:Accessor.cs

示例12: GetCodeIssues

            public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode, 
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate, 
                Violation violation, 
                CsElement csElement)
            {
                int prefixLength = "The call to ".Length;
                var memberName = violation.Message.Substring(prefixLength, violation.Message.IndexOf(" must") - prefixLength);
                var thisFound = false;
                foreach (var token in from token in csElement.ElementTokens
                                      where token.LineNumber == violation.Line && token.CsTokenType != CsTokenType.WhiteSpace
                                      select token)
                {
                    if (token.CsTokenType == CsTokenType.This || (thisFound && token.Text == "."))
                    {
                        thisFound = true;
                    }
                    else
                    {
                        if (token.Text == memberName && !thisFound)
                        {
                            var sourceRange = new SourceRange(token.Location.StartPoint.LineNumber, token.Location.StartPoint.IndexOnLine + 1, token.Location.EndPoint.LineNumber, token.Location.EndPoint.IndexOnLine + 2);
                            yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, sourceRange);
                        }

                        thisFound = false;
                    }
                }
            }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:29,代码来源:SA1101_PrefixLocalCallsWithThis.cs

示例13: Method

 internal Method(CsDocument document, CsElement parent, XmlHeader header, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes, Declaration declaration, TypeToken returnType, IList<Parameter> parameters, ICollection<TypeParameterConstraintClause> typeConstraints, bool unsafeCode, bool generated)
     : base(document, parent, ElementType.Method, "method " + declaration.Name, header, attributes, declaration, unsafeCode, generated)
 {
     this.returnType = returnType;
     this.parameters = parameters;
     this.typeConstraints = typeConstraints;
     if ((this.parameters.Count > 0) && base.Declaration.ContainsModifier(new CsTokenType[] { CsTokenType.Static }))
     {
         foreach (Parameter parameter in this.parameters)
         {
             if ((parameter.Modifiers & ParameterModifiers.This) != ParameterModifiers.None)
             {
                 this.extensionMethod = true;
             }
             break;
         }
     }
     base.QualifiedName = CodeParser.AddQualifications(this.parameters, base.QualifiedName);
     if ((base.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1) && !base.Declaration.Name.StartsWith("this.", StringComparison.Ordinal))
     {
         base.Declaration.AccessModifierType = AccessModifierType.Public;
     }
     if (typeConstraints != null)
     {
         foreach (TypeParameterConstraintClause clause in typeConstraints)
         {
             clause.ParentElement = this;
         }
     }
 }
开发者ID:katerina-marchenkova,项目名称:my,代码行数:30,代码来源:Method.cs

示例14: GetCodeIssues

 public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
     ISourceCode sourceCode,
     Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
     Violation violation,
     CsElement csElement)
 {
     foreach (var operation in from x in enumerate(new ElementTypeFilter(LanguageElementType.LogicalOperation))
                                            let logicalOperation = (LogicalOperation)x.ToLanguageElement()
                                            where logicalOperation.RecoveredRange.Start.Line == violation.Line
                                                 && (logicalOperation.LeftSide.ElementType == LanguageElementType.LogicalOperation
                                                      || logicalOperation.RightSide.ElementType == LanguageElementType.LogicalOperation)
                                            select logicalOperation)
     {
         var parentType = operators[operation.Name];
         var leftChildType = operation.LeftSide.ElementType == LanguageElementType.LogicalOperation ? operators[operation.LeftSide.Name] : parentType;
         var rightChildType = operation.RightSide.ElementType == LanguageElementType.LogicalOperation ? operators[operation.RightSide.Name] : parentType;
         if (rightChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.RightSide.RecoveredRange);
         }
         else if (leftChildType > parentType)
         {
             yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, operation.LeftSide.RecoveredRange);
         }
     }
 }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:26,代码来源:SA1408_ConditionalExpressionsMustDeclarePrecedence.cs

示例15: GetCodeIssues

            public IEnumerable<StyleCopCodeIssue> GetCodeIssues(
                ISourceCode sourceCode,
                Func<ElementTypeFilter, IEnumerable<IElement>> enumerate,
                Violation violation,
                CsElement csElement)
            {
                foreach (var method in from x in enumerate(new ElementTypeFilter(LanguageElementType.Method))
                                       let method = (DX.Method)x.ToLanguageElement()
                                       where method.RecoveredRange.Start.Line == violation.Line
                                          && method.IsStatic && method.IsConstructor
                                          && method.FirstChild == null
                                       select method)
                {
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, method.NameRange);
                }

                foreach (var statement in from x in enumerate(new ElementTypeFilter(deleteWhenEmpty))
                                          let statement = x.ToLanguageElement()
                                          where statement.RecoveredRange.Start.Line == violation.Line
                                             && (statement.ElementType == LanguageElementType.Try || statement.FirstChild == null)
                                          select statement)
                {
                    yield return new StyleCopCodeIssue(CodeIssueType.CodeSmell, statement.GetKeywordRange());
                }
            }
开发者ID:kevinmiles,项目名称:dxcorecommunityplugins,代码行数:25,代码来源:SA1409_RemoveUnnecessaryCode.cs


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