예제 #1
0
        public async Task <CompilationUnitSyntax> GenerateCode(CancellationToken cancellationToken)
        {
            var namespaceName = "HagarGeneratedCode." + this.compilation.AssemblyName;

            // Collect metadata from the compilation.
            var metadataModel = await this.GenerateMetadataModel(cancellationToken);

            var members = new List <MemberDeclarationSyntax>();

            foreach (var type in metadataModel.InvokableInterfaces)
            {
                foreach (var method in type.Methods)
                {
                    var(invokable, generatedInvokerDescription) = InvokableGenerator.Generate(this.compilation, this.libraryTypes, type, method);
                    metadataModel.SerializableTypes.Add(generatedInvokerDescription);
                    metadataModel.GeneratedInvokables[method] = generatedInvokerDescription;
                    members.Add(invokable);
                }

                var(proxy, generatedProxyDescription) = ProxyGenerator.Generate(this.compilation, this.libraryTypes, type, metadataModel);
                metadataModel.GeneratedProxies.Add(generatedProxyDescription);
                members.Add(proxy);
            }

            // Generate code.
            foreach (var type in metadataModel.SerializableTypes)
            {
                // Generate a partial serializer class for each serializable type.
                members.Add(SerializerGenerator.GenerateSerializer(this.compilation, this.libraryTypes, type));

                if (type.IsEmptyConstructable)
                {
                    metadataModel.ActivatableTypes.Add(type);

                    // Generate a partial serializer class for each serializable type.
                    members.Add(ActivatorGenerator.GenerateActivator(this.compilation, this.libraryTypes, type));
                }
            }

            // Generate metadata.
            var metadataClass = MetadataGenerator.GenerateMetadata(this.compilation, metadataModel, this.libraryTypes);

            members.Add(metadataClass);

            var metadataAttribute = AttributeList()
                                    .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))
                                    .WithAttributes(
                SingletonSeparatedList(
                    Attribute(this.libraryTypes.MetadataProviderAttribute.ToNameSyntax())
                    .AddArgumentListArguments(AttributeArgument(TypeOfExpression(ParseTypeName($"{namespaceName}.{metadataClass.Identifier.Text}"))))));

            return(CompilationUnit()
                   .WithAttributeLists(List(new [] { metadataAttribute }))
                   .WithMembers(
                       SingletonList <MemberDeclarationSyntax>(
                           NamespaceDeclaration(ParseName(namespaceName))
                           .WithMembers(List(members))
                           .WithUsings(List(new[] { UsingDirective(ParseName("global::Hagar.Codecs")), UsingDirective(ParseName("global::Hagar.GeneratedCodeHelpers")) })))));
        }
예제 #2
0
        public static TypeSyntax GetInvokableTypeName(this MethodDescription method)
        {
            var genericArity = method.Method.TypeParameters.Length + method.Method.ContainingType.TypeParameters.Length;
            var name         = InvokableGenerator.GetSimpleClassName(method.Method);

            if (genericArity > 0)
            {
                name += $"<{new string(',', genericArity - 1)}>";
            }

            return(ParseTypeName(name));
        }