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


C# PropertyDeclarationSyntax.GetEffectiveAccessibility方法代码示例

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


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

示例1: AnalyzeSummaryElement

        private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, XmlNodeSyntax syntax, Location diagnosticLocation, PropertyDeclarationSyntax propertyDeclaration, string startingTextGets, string startingTextSets, string startingTextGetsOrSets)
        {
            var diagnosticProperties = ImmutableDictionary.CreateBuilder<string, string>();
            ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;
            AccessorDeclarationSyntax getter = null;
            AccessorDeclarationSyntax setter = null;

            if (propertyDeclaration.AccessorList != null)
            {
                foreach (var accessor in propertyDeclaration.AccessorList.Accessors)
                {
                    switch (accessor.Keyword.Kind())
                    {
                    case SyntaxKind.GetKeyword:
                        getter = accessor;
                        break;

                    case SyntaxKind.SetKeyword:
                        setter = accessor;
                        break;
                    }
                }
            }

            XmlElementSyntax summaryElement = (XmlElementSyntax)syntax;
            if (summaryElement == null)
            {
                // This is reported by SA1604.
                return;
            }

            // Add a no code fix tag when the summary element is empty.
            // This will only impact SA1623, because SA1624 cannot trigger with an empty summary.
            if (summaryElement.Content.Count == 0)
            {
                diagnosticProperties.Add(NoCodeFixKey, string.Empty);
            }

            var textElement = summaryElement.Content.FirstOrDefault() as XmlTextSyntax;
            var text = textElement == null ? string.Empty : XmlCommentHelper.GetText(textElement, true).TrimStart();

            if (getter != null || expressionBody != null)
            {
                bool startsWithGetOrSet = text.StartsWith(startingTextGetsOrSets, StringComparison.Ordinal);

                if (setter != null)
                {
                    // There is no way getter is null (can't have expression body and accessor list)
                    bool getterVisible;
                    bool setterVisible;

                    if (!getter.Modifiers.Any() && !setter.Modifiers.Any())
                    {
                        // Case 1: The getter and setter have the same declared accessibility
                        getterVisible = true;
                        setterVisible = true;
                    }
                    else if (getter.Modifiers.Any(SyntaxKind.PrivateKeyword))
                    {
                        // Case 3
                        getterVisible = false;
                        setterVisible = true;
                    }
                    else if (setter.Modifiers.Any(SyntaxKind.PrivateKeyword))
                    {
                        // Case 3
                        getterVisible = true;
                        setterVisible = false;
                    }
                    else
                    {
                        var propertyAccessibility = propertyDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
                        bool propertyOnlyInternal = propertyAccessibility == Accessibility.Internal
                            || propertyAccessibility == Accessibility.ProtectedAndInternal
                            || propertyAccessibility == Accessibility.Private;
                        if (propertyOnlyInternal)
                        {
                            // Case 2: Property only internal and no accessor is explicitly private
                            getterVisible = true;
                            setterVisible = true;
                        }
                        else
                        {
                            var getterAccessibility = getter.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
                            var setterAccessibility = setter.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);

                            switch (getterAccessibility)
                            {
                            case Accessibility.Public:
                            case Accessibility.ProtectedOrInternal:
                            case Accessibility.Protected:
                                // Case 4
                                getterVisible = true;
                                break;

                            case Accessibility.Internal:
                            case Accessibility.ProtectedAndInternal:
                            case Accessibility.Private:
                            default:
                                // The property is externally accessible, so the setter must be more accessible.
//.........这里部分代码省略.........
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:PropertySummaryDocumentationAnalyzer.cs


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