public void Execute(SourceGeneratorContext context) { context.AddCompiledOnMetadataAttribute(); var compilation = context.Compilation; var types = CompilationHelper.GetAllTypes(context.Compilation.Assembly); using (var stringWriter = new StringWriter()) using (var indentedTextWriter = new IndentedTextWriter(stringWriter, " ")) { var defaultToStringGenerator = new DefaultToStringGenerator(context); foreach (var type in types) { if (DefaultToStringGenerator.ShouldUseGenerator(type)) { defaultToStringGenerator.WriteType(type, indentedTextWriter); } } indentedTextWriter.Flush(); stringWriter.Flush(); var sourceText = SourceText.From(stringWriter.ToString(), Encoding.UTF8); var hintName = $"AutoToString_{compilation.Assembly.Name}.g.cs"; context.AddSource(hintName, sourceText); } }