Пример #1
0
        /// <summary>
        /// Generates the empty stunt code as a <see cref="SyntaxNode"/>, also returning
        /// the resulting class name. It contains just the main compilation unit, namespace
        /// imports, namespace declaration and class declaration with the given base type and
        /// interfaces, with no class members at all.
        /// </summary>
        public (string name, SyntaxNode syntax) CreateStunt(IEnumerable <INamedTypeSymbol> symbols, SyntaxGenerator generator)
        {
            var name    = naming.GetName(symbols);
            var imports = new HashSet <string>();

            var(baseType, implementedInterfaces) = symbols.ValidateGeneratorTypes();

            AddImports(imports, baseType);
            foreach (var iface in implementedInterfaces)
            {
                AddImports(imports, iface);
            }

            var syntax = generator.CompilationUnit(imports
                                                   .Select(generator.NamespaceImportDeclaration)
                                                   .Concat(new[]
            {
                generator.NamespaceDeclaration(naming.Namespace,
                                               generator.AddAttributes(
                                                   generator.ClassDeclaration(name,
                                                                              modifiers: DeclarationModifiers.Partial,
                                                                              baseType: baseType == null ? null : AsSyntaxNode(generator, baseType),
                                                                              interfaceTypes: implementedInterfaces
                                                                              .Select(x => AsSyntaxNode(generator, x))
                                                                              )
                                                   )
                                               )
            }));

            return(name, syntax);
        }
Пример #2
0
        /// <summary>
        /// Generates the empty stunt code as a <see cref="SyntaxNode"/>, also returning
        /// the resulting class name. It contains just the main compilation unit, namespace
        /// imports, namespace declaration and class declaration with the given base type and
        /// interfaces, with no class members at all.
        /// </summary>
        public (string name, SyntaxNode syntax) CreateStunt(IEnumerable <INamedTypeSymbol> symbols, SyntaxGenerator generator)
        {
            var name    = naming.GetName(symbols);
            var imports = new HashSet <string>();

            var(baseType, implementedInterfaces) = symbols.ValidateGeneratorTypes();

            if (baseType != null && baseType.ContainingNamespace != null && baseType.ContainingNamespace.CanBeReferencedByName)
            {
                imports.Add(baseType.ContainingNamespace.ToDisplayString());
            }

            foreach (var iface in implementedInterfaces.Where(i => i.ContainingNamespace != null && i.ContainingNamespace.CanBeReferencedByName))
            {
                imports.Add(iface.ContainingNamespace.ToDisplayString());
            }

            var syntax = generator.CompilationUnit(imports
                                                   .Select(generator.NamespaceImportDeclaration)
                                                   .Concat(new[]
            {
                generator.NamespaceDeclaration(naming.Namespace,
                                               generator.AddAttributes(
                                                   generator.ClassDeclaration(name,
                                                                              accessibility: Accessibility.Public,
                                                                              modifiers: DeclarationModifiers.Partial,
                                                                              baseType: baseType == null ? null : generator.IdentifierName(baseType.Name),
                                                                              interfaceTypes: implementedInterfaces
                                                                              .Select(x => generator.IdentifierName(x.ContainingType != null
                                        ? x.ContainingType.Name + "." + x.Name
                                        : x.Name))
                                                                              )
                                                   )
                                               )
            }));

            return(name, syntax);
        }