public static ClassDeclarationSyntax GenerateSerializer(Compilation compilation, TypeDescription typeDescription) { var type = typeDescription.Type; var simpleClassName = GetSimpleClassName(type); var libraryTypes = LibraryTypes.FromCompilation(compilation); var serializerInterface = type.IsValueType ? libraryTypes.ValueSerializer : libraryTypes.PartialSerializer; var baseInterface = serializerInterface.Construct(type).ToTypeSyntax(); var fieldDescriptions = GetFieldDescriptions(typeDescription, libraryTypes); var fields = GetFieldDeclarations(fieldDescriptions); var ctor = GenerateConstructor(simpleClassName, fieldDescriptions); var serializeMethod = GenerateSerializeMethod(typeDescription, fieldDescriptions, libraryTypes); var deserializeMethod = GenerateDeserializeMethod(typeDescription, fieldDescriptions, libraryTypes); var classDeclaration = ClassDeclaration(simpleClassName) .AddBaseListTypes(SimpleBaseType(baseInterface)) .AddModifiers(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)) .AddAttributeLists(AttributeList(SingletonSeparatedList(CodeGenerator.GetGeneratedCodeAttributeSyntax()))) .AddMembers(fields) .AddMembers(ctor, serializeMethod, deserializeMethod); if (type.IsGenericType) { classDeclaration = AddGenericTypeConstraints(classDeclaration, type); } return(classDeclaration); }
public CodeGenerator(Compilation compilation, CodeGeneratorOptions options) { this.compilation = compilation; this.options = options; this.libraryTypes = LibraryTypes.FromCompilation(compilation, options); if (options.GenerateSerializerAttributes != null) { this.generateSerializerAttributes = options.GenerateSerializerAttributes.Select(compilation.GetTypeByMetadataName).ToArray(); } }
public static ClassDeclarationSyntax GenerateMetadata(Compilation compilation, List <TypeDescription> serializableTypes) { var configParam = "config".ToIdentifierName(); var addMethod = configParam.Member("PartialSerializers").Member("Add"); var body = new List <StatementSyntax>(); body.AddRange( serializableTypes.Select( type => (StatementSyntax)ExpressionStatement(InvocationExpression(addMethod, ArgumentList(SingletonSeparatedList(Argument(TypeOfExpression(GetPartialSerializerTypeName(type.Type))))))) )); var libraryTypes = LibraryTypes.FromCompilation(compilation); var configType = libraryTypes.SerializerConfiguration; var configureMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), "Configure") .AddModifiers(Token(SyntaxKind.PublicKeyword)) .AddParameterListParameters( Parameter(configParam.Identifier).WithType(configType.ToTypeSyntax())) .AddBodyStatements(body.ToArray()); var interfaceType = libraryTypes.ConfigurationProvider.Construct(configType); return(ClassDeclaration(CodeGenerator.CodeGeneratorName + "_Metadata_" + compilation.AssemblyName.Replace('.', '_')) .AddBaseListTypes(SimpleBaseType(interfaceType.ToTypeSyntax())) .AddModifiers(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword)) .AddAttributeLists(AttributeList(SingletonSeparatedList(CodeGenerator.GetGeneratedCodeAttributeSyntax()))) .AddMembers(configureMethod)); TypeSyntax GetPartialSerializerTypeName(INamedTypeSymbol type) { var genericArity = type.TypeParameters.Length; var name = PartialSerializerGenerator.GetSimpleClassName(type); if (genericArity > 0) { name += $"<{new string(',', genericArity - 1)}>"; } return(ParseTypeName(name)); } }