Exemple #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected ISyntaxEmitter CreateMemberSyntaxEmitter(AbstractMember member)
        {
            if (member is FieldMember field)
            {
                return(new FieldSyntaxEmitter(field));
            }

            if (member is ConstructorMember constructor)
            {
                return(new ConstructorSyntaxEmitter(constructor));
            }

            if (member is MethodMember method)
            {
                return(new MethodSyntaxEmitter(method));
            }

            if (member is PropertyMember property)
            {
                return(new PropertySyntaxEmitter(property));
            }

            if (member is EventMember @event)
            {
                return(new EventSyntaxEmitter(@event));
            }

            if (member is TypeMember type)
            {
                return(TypeSyntaxEmitter.GetSyntaxEmitter(type));
            }

            throw new ArgumentException($"Syntax emitter is not supported for members of type {member.GetType().Name}");
        }
Exemple #2
0
        public SyntaxTree EmitSyntax()
        {
            DetermineNamespaceImports(GetReferencedTypes(), out var usingSyntaxList, out var importContext);

            using (CodeGeneratorContext.GetContextOrThrow().PushState(importContext))
            {
                var typeSyntax = TypeSyntaxEmitter.GetSyntax(_type);
                var unitSyntax = SyntaxFactory.CompilationUnit()
                                 .WithUsings(SyntaxFactory.List(usingSyntaxList))
                                 .WithMembers(
                    SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                        SyntaxFactory.NamespaceDeclaration(CreateQualifiedNameSyntax(_type.Namespace.Split('.')))
                        .WithMembers(SyntaxFactory.SingletonList(typeSyntax))))
                                 .NormalizeWhitespace();

                return(unitSyntax.SyntaxTree);
            }
        }