Esempio n. 1
0
        private static CacheBuilderOptions InitializeOptions(CacheBuilderOptions options, Type type)
        {
            options = options ?? new CacheBuilderOptions();
            options.GeneratedTypeName  = options.GeneratedTypeName ?? $"{type.Name}Factory";
            options.GeneratedNamespace = options.GeneratedNamespace ?? "CompiledCache";

            return(options);
        }
Esempio n. 2
0
        public static SyntaxTree CreateCache <T>(T item, CacheBuilderOptions options = default)
        {
            var type = typeof(T);

            options = InitializeOptions(options, type);

            var newClass = SyntaxFactory.ClassDeclaration(options.GeneratedTypeName)
                           .AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
                           .AddMembers(
                GenerateCreateObjectMethod(),
                SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(type.FullName), "Create")
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
                .WithBody(SyntaxFactory.Block(GenerateStatements(item))));

            var newNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName(options.GeneratedNamespace))
                               .AddMembers(newClass)
                               .NormalizeWhitespace();

            return(SyntaxFactory.SyntaxTree(newNamespace));
        }