示例#1
0
        internal static Document Execute(CodeFixExecutionContext <AttributeSyntax> context, ITypeSymbol targetType)
        {
            INamespaceSymbol? @namespace = (context.SemanticModel.GetSymbolInfo(context.Node).Symbol?.ContainingNamespace) ?? context.Compilation.GlobalNamespace;

            NameSyntax name = context.SemanticModel.GetNameSyntax(context.Root.Usings, @namespace, targetType, context.CancellationToken);

            AttributeSyntax attr = context.Node
                                   .WithArgumentList(SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(
                                                                                             SyntaxFactory.AttributeArgument(SyntaxFactory.TypeOfExpression(name)))));

            context.RegisterChange(context.Node, attr);
            return(context.Document);
        }
        private static Task <Document> ExecuteAsync(CodeFixExecutionContext <TypeParameterSyntax> context, ITypeSymbol targetType, INamedTypeSymbol attribute)
        {
            INamespaceSymbol? @namespace = (context.SemanticModel.GetDeclaredSymbol(context.Node)?.ContainingNamespace) ?? context.Compilation.GlobalNamespace;

            NameSyntax attributeName = context.SemanticModel.GetNameSyntaxForAttribute(context.Root.Usings, @namespace, attribute, context.CancellationToken);

            TypeParameterSyntax parameter = context.Node.AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Attribute(attributeName))));

            context.RegisterChange(parameter);

            AttributeSyntax attr = context.Node.AttributeLists.Last().Attributes[0];

            return(Task.FromResult(MakeValueOfOverridenAttributeEquivalentCodeFix.Execute(context.WithNode(attr), targetType)));
        }
        private static Task <Document> ExecuteAsync(CodeFixExecutionContext <TypeDeclarationSyntax> context, INamedTypeSymbol attribute)
        {
            INamespaceSymbol? @namespace = (context.SemanticModel.GetSymbolInfo(context.Node).Symbol?.ContainingNamespace) ?? context.Compilation.GlobalNamespace;

            NameSyntax attrName;
            NameSyntax enumName;

            if (context.SemanticModel.HasUsingDirective(context.Root.Usings, @namespace, attribute, context.CancellationToken))
            {
                attrName = SyntaxFactory.IdentifierName("DefaultParamConfiguration");
                enumName = SyntaxFactory.IdentifierName(nameof(TypeConvention));
            }
            else
            {
                QualifiedNameSyntax n =
                    SyntaxFactory.QualifiedName(
                        SyntaxFactory.IdentifierName("Durian"),
                        SyntaxFactory.IdentifierName("Configuration"));

                attrName = SyntaxFactory.QualifiedName(n, SyntaxFactory.IdentifierName("DefaultParamConfiguration"));
                enumName = SyntaxFactory.QualifiedName(n, SyntaxFactory.IdentifierName(nameof(TypeConvention)));
            }

            TypeDeclarationSyntax type = context.Node.AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
                                                                                                        SyntaxFactory.Attribute(attrName,
                                                                                                                                SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(
                                                                                                                                                                        SyntaxFactory.AttributeArgument(
                                                                                                                                                                            SyntaxFactory.MemberAccessExpression(
                                                                                                                                                                                SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                                                                                enumName,
                                                                                                                                                                                SyntaxFactory.IdentifierName(nameof(TypeConvention.Copy))))
                                                                                                                                                                        .WithNameEquals(
                                                                                                                                                                            SyntaxFactory.NameEquals(
                                                                                                                                                                                SyntaxFactory.IdentifierName(MemberNames.Config_TypeConvention),
                                                                                                                                                                                SyntaxFactory.Token(SyntaxKind.EqualsToken).WithTrailingTrivia(SyntaxFactory.Space)))))))));

            context.RegisterChange(context.Node, type);
            return(Task.FromResult(context.Document));
        }