Пример #1
0
        public CompilationUnitSyntax StubInterface(CompilationUnitSyntax cu, InterfaceDeclarationSyntax interfaceDclr,
                                                   SemanticModel semanticModel)
        {
            INamedTypeSymbol           interfaceType = semanticModel.GetDeclaredSymbol(interfaceDclr);
            NamespaceDeclarationSyntax namespaceNode = GetNamespaceNode(interfaceDclr);
            string interfaceName             = interfaceType.GetGenericName();
            string stubName                  = NamingUtils.GetStubName(interfaceName);
            ClassDeclarationSyntax classDclr = SF.ClassDeclaration(SF.Identifier(stubName))
                                               .AddModifiers(SF.Token(RoslynUtils.GetVisibilityKeyword(interfaceType)))
                                               .WithBaseList(RoslynUtils.BaseList(interfaceName))
                                               .AddAttributeLists(AttributeListList(Attribute("CompilerGenerated")).ToArray());

            classDclr = RoslynUtils.CopyGenericConstraints(interfaceType, classDclr);
            classDclr = AddStubContainerField(classDclr, stubName);
            classDclr = StubProperties(interfaceType, classDclr);
            classDclr = StubMethods(interfaceType, classDclr);

            string fullNameSpace = semanticModel.GetDeclaredSymbol(namespaceNode).ToString();
            NamespaceDeclarationSyntax namespaceDclr = SF.NamespaceDeclaration(SF.IdentifierName(fullNameSpace))
                                                       .WithUsings(namespaceNode.Usings);

            namespaceDclr = namespaceDclr.AddMembers(classDclr);
            cu            = cu.AddMembers(namespaceDclr);
            return(cu);
        }
Пример #2
0
        private static DelegateDeclarationSyntax GenerateDelegateDclr(IMethodSymbol methodSymbol, string delegateName,
                                                                      INamedTypeSymbol stubbedInterface)
        {
            SyntaxKind                visibility          = RoslynUtils.GetVisibilityKeyword(stubbedInterface);
            List <ParameterSyntax>    paramsSyntaxList    = RoslynUtils.GetMethodParameterSyntaxList(methodSymbol);
            DelegateDeclarationSyntax delegateDeclaration = SF.DelegateDeclaration(SF.ParseTypeName(methodSymbol.ReturnType.GetFullyQualifiedName()),
                                                                                   delegateName)
                                                            .AddModifiers(SF.Token(visibility)).AddParameterListParameters(paramsSyntaxList.ToArray());

            delegateDeclaration = RoslynUtils.CopyGenericConstraints(methodSymbol, delegateDeclaration);
            return(delegateDeclaration);
        }
Пример #3
0
        private static MethodDeclarationSyntax GenerateSetupMethod(IMethodSymbol methodSymbol, string setupMethodName, string delegateTypeName,
                                                                   INamedTypeSymbol stubbedInterface,
                                                                   ClassDeclarationSyntax stub)
        {
            SyntaxKind visibility = RoslynUtils.GetVisibilityKeyword(stubbedInterface);
            MethodDeclarationSyntax methodDclr = SF.MethodDeclaration(SF.ParseTypeName(stub.Identifier.Text), setupMethodName)
                                                 .AddModifiers(SF.Token(visibility)).WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken))
                                                 .AddParameterListParameters(
                SF.Parameter(SF.Identifier("del")).WithType(SF.ParseTypeName(delegateTypeName)),
                SF.Parameter(SF.Identifier("count")).WithType(SF.ParseTypeName("int")).WithDefault(SF.EqualsValueClause(SF.ParseExpression("Times.Forever"))),
                SF.Parameter(SF.Identifier("overwrite")).WithType(SF.ParseTypeName("bool")).WithDefault(SF.EqualsValueClause(SF.ParseExpression("false")))
                )
                                                 .WithBody(SF.Block(
                                                               SF.ParseStatement("_stubs.SetMethodStub(del, count, overwrite);\n"),
                                                               SF.ParseStatement("return this;\n")
                                                               ))
                                                 .WithSemicolonToken(SF.Token(SyntaxKind.None));

            return(RoslynUtils.CopyGenericConstraints(methodSymbol, methodDclr));
        }