private async Task <Document> MakePublic(Document document, SyntaxNode getMethod, SyntaxNode property, CancellationToken cancellationToken)
        {
            // Clear the accessibility on the getter.
            DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            editor.SetAccessibility(getMethod, Accessibility.NotApplicable);

            // If the containing property is not public, make it so
            Accessibility propertyAccessibility = editor.Generator.GetAccessibility(property);

            if (propertyAccessibility != Accessibility.Public)
            {
                editor.SetAccessibility(property, Accessibility.Public);

                // Having just made the property public, if it has a setter with no Accessibility set, then we've just made the setter public.
                // Instead restore the setter's original accessibility so that we don't fire a violation with the generated code.
                SyntaxNode setter = editor.Generator.GetAccessor(property, DeclarationKind.SetAccessor);
                if (setter != null)
                {
                    Accessibility setterAccessibility = editor.Generator.GetAccessibility(setter);
                    if (setterAccessibility == Accessibility.NotApplicable)
                    {
                        editor.SetAccessibility(setter, propertyAccessibility);
                    }
                }
            }

            return(editor.GetChangedDocument());
        }
        private async Task <Document> RemoveSetter(Document document, SyntaxNode setMethod, CancellationToken cancellationToken)
        {
            DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            editor.SetAccessibility(setMethod, Accessibility.Internal);
            return(editor.GetChangedDocument());
        }
예제 #3
0
        private static void ChangeMemberToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            SyntaxNode memberDeclaration,
            ISymbol member
            )
        {
            var modifiers = DeclarationModifiers.From(member).WithIsStatic(false);

            // Event is different since several events may be declared in one line.
            if (member is IEventSymbol eventSymbol)
            {
                ChangeEventToPublicAndNonStatic(
                    codeGenerationService,
                    editor,
                    eventSymbol,
                    memberDeclaration,
                    modifiers
                    );
            }
            else
            {
                editor.SetAccessibility(memberDeclaration, Accessibility.Public);
                editor.SetModifiers(memberDeclaration, modifiers);
            }
        }
        private async Task <Document> MakePublic(Document document, SyntaxNode getMethod, CancellationToken cancellationToken)
        {
            DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            editor.SetAccessibility(getMethod, Accessibility.Public);
            return(editor.GetChangedDocument());
        }
예제 #5
0
        private static void IncreaseVisibility(DocumentEditor editor, SyntaxNode declaration, Accessibility targetAccessibility)
        {
            if (declaration == null)
            {
                return;
            }
            ISymbol symbol   = editor.SemanticModel.GetDeclaredSymbol(declaration);
            var     property = (symbol as IMethodSymbol)?.AssociatedSymbol as IPropertySymbol;

            if (property != null && IsMoreRestrictive(property.DeclaredAccessibility, targetAccessibility))
            {
                // Can't explicitly set an accessor to a visibility greater than or equal to that of the containing property
                editor.SetAccessibility(declaration, Accessibility.NotApplicable);
            }
            else
            {
                editor.SetAccessibility(declaration, targetAccessibility);
            }
        }
예제 #6
0
        private static void ChangeMemberToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            SyntaxNode memberDeclaration,
            ISymbol member)
        {
            var modifiers = DeclarationModifiers.From(member).WithIsStatic(false);

            editor.SetAccessibility(memberDeclaration, Accessibility.Public);
            editor.SetModifiers(memberDeclaration, modifiers);
        }
예제 #7
0
        private static void ChangeEventToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            IEventSymbol eventSymbol,
            SyntaxNode eventDeclaration,
            DeclarationModifiers modifiers
            )
        {
            var declaration = editor.Generator.GetDeclaration(eventDeclaration);
            var isEventHasExplicitAddOrRemoveMethod =
                (eventSymbol.AddMethod != null && !eventSymbol.AddMethod.IsImplicitlyDeclared) ||
                (
                    eventSymbol.RemoveMethod != null &&
                    !eventSymbol.RemoveMethod.IsImplicitlyDeclared
                );

            // There are three situations here:
            // 1. Single Event.
            // 2. Several events exist in one declaration.
            // 3. Event has add or remove method(user declared).
            // For situation 1, declaration is EventFieldDeclaration, eventDeclaration is variableDeclaration.
            // For situation 2, declaration and eventDeclaration are both EventDeclaration, which are same.
            // For situation 3, it is same as situation 2, but has add or remove method.
            if (declaration.Equals(eventDeclaration) && !isEventHasExplicitAddOrRemoveMethod)
            {
                // Several events are declared in same line
                var publicAndNonStaticSymbol = CodeGenerationSymbolFactory.CreateEventSymbol(
                    eventSymbol,
                    accessibility: Accessibility.Public,
                    modifiers: modifiers
                    );
                var options = new CodeGenerationOptions(generateMethodBodies: false);
                var publicAndNonStaticSyntax = codeGenerationService.CreateEventDeclaration(
                    publicAndNonStaticSymbol,
                    destination: CodeGenerationDestination.ClassType,
                    options: options
                    );
                // Insert a new declaration and remove the original declaration
                editor.InsertAfter(declaration, publicAndNonStaticSyntax);
                editor.RemoveNode(eventDeclaration);
            }
            else
            {
                // Handle both single event and event has add or remove method
                editor.SetAccessibility(declaration, Accessibility.Public);
                editor.SetModifiers(declaration, modifiers);
            }
        }