コード例 #1
0
        private static INamespaceOrTypeSymbol CreateCodeGenerationSymbol(
            Document document,
            ISymbol symbol
            )
        {
            symbol = symbol.GetOriginalUnreducedDefinition();
            var topLevelNamespaceSymbol = symbol.ContainingNamespace;
            var topLevelNamedType       = MetadataAsSourceHelpers.GetTopLevelContainingNamedType(symbol);

            var canImplementImplicitly =
                document.GetLanguageService <ISemanticFactsService>().SupportsImplicitInterfaceImplementation;
            var docCommentFormattingService =
                document.GetLanguageService <IDocumentationCommentFormattingService>();

            INamespaceOrTypeSymbol wrappedType = new WrappedNamedTypeSymbol(
                topLevelNamedType,
                canImplementImplicitly,
                docCommentFormattingService
                );

            return(topLevelNamespaceSymbol.IsGlobalNamespace
              ? wrappedType
              : CodeGenerationSymbolFactory.CreateNamespaceSymbol(
                       topLevelNamespaceSymbol.ToDisplayString(SymbolDisplayFormats.NameFormat),
                       null,
                       new[] { wrappedType }
                       ));
        }
コード例 #2
0
        protected SyntaxNode CreateNamespaceDeclaration(SyntaxNode containerNode, string name)
        {
            var destination = CodeModelService.GetDestination(containerNode);

            var newNamespaceSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol(name);

            var newNamespace = CodeGenerationService.CreateNamespaceDeclaration(
                newNamespaceSymbol, destination);

            return(newNamespace);
        }
コード例 #3
0
        public static INamespaceOrTypeSymbol GenerateRootNamespaceOrType(this INamedTypeSymbol namedType, string[] containers)
        {
            INamespaceOrTypeSymbol currentSymbol = namedType;

            for (int i = containers.Length - 1; i >= 0; i--)
            {
                currentSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol(containers[i], members: new[] { currentSymbol });
            }

            return(currentSymbol);
        }
コード例 #4
0
        public async Task TestThrowsOnGenerateNamespace()
        {
            var namespaceSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol("Outerspace");

            using (var context = TestContext.Create())
            {
                await Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    await context.GenerateSourceAsync(namespaceSymbol);
                });
            }
        }
コード例 #5
0
 internal static async Task TestAddNamespaceAsync(
     string initial,
     string expected,
     string name = "N",
     IList<ISymbol> imports = null,
     IList<INamespaceOrTypeSymbol> members = null,
     CodeGenerationOptions codeGenerationOptions = null)
 {
     using var context = await TestContext.CreateAsync(initial, expected);
     var @namespace = CodeGenerationSymbolFactory.CreateNamespaceSymbol(name, imports, members);
     context.Result = await context.Service.AddNamespaceAsync(context.Solution, (INamespaceSymbol)context.GetDestination(), @namespace, codeGenerationOptions);
 }
コード例 #6
0
ファイル: CodeGenerationTests.cs プロジェクト: Forgind/roslyn
        internal static async Task TestAddNamespaceAsync(
            string initial,
            string expected,
            string name             = "N",
            IList <ISymbol> imports = null,
            IList <INamespaceOrTypeSymbol> members = null,
            CodeGenerationContext context          = null)
        {
            using var testContext = await TestContext.CreateAsync(initial, expected);

            var @namespace = CodeGenerationSymbolFactory.CreateNamespaceSymbol(name, imports, members);

            testContext.Result = await testContext.Service.AddNamespaceAsync(testContext.Solution, (INamespaceSymbol)testContext.GetDestination(), @namespace, context ?? CodeGenerationContext.Default, CancellationToken.None);
        }
コード例 #7
0
 internal static void TestAddNamespace(
     string initial,
     string expected,
     string name             = "N",
     IList <ISymbol> imports = null,
     IList <INamespaceOrTypeSymbol> members      = null,
     CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
     bool compareTokens = true)
 {
     using (var context = new TestContext(initial, expected, compareTokens))
     {
         var @namespace = CodeGenerationSymbolFactory.CreateNamespaceSymbol(name, imports, members);
         context.Result = context.Service.AddNamespaceAsync(context.Solution, (INamespaceSymbol)context.GetDestination(), @namespace, codeGenerationOptions).Result;
     }
 }
コード例 #8
0
        public void TestThrowsOnGenerateNamespace()
        {
            var namespaceSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol("Outerspace");

            using (var context = new TestContext())
            {
                Assert.Throws <ArgumentException>(() =>
                {
                    try
                    {
                        context.GenerateSource(namespaceSymbol);
                    }
                    catch (AggregateException ae)
                    {
                        throw ae.InnerException;
                    }
                });
            }
        }