public void Execute(GeneratorExecutionContext context) { MySyntaxReceiver syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver; CSharpParseOptions options = (context.Compilation as CSharpCompilation).SyntaxTrees[0].Options as CSharpParseOptions; Compilation compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(attributeText, Encoding.UTF8), options)); INamedTypeSymbol attributeSymbol = compilation.GetTypeByMetadataName("PrintableFields.PrintableAttribute"); ProcessPrintableAttributedFields(context, syntaxReceiver, compilation, attributeSymbol); ProcessPrintAllFieldPartialMethods(context, syntaxReceiver, compilation); }
private void ProcessPrintableAttributedFields(GeneratorExecutionContext context, MySyntaxReceiver syntaxReceiver, Compilation compilation, INamedTypeSymbol attributeSymbol) { List <IFieldSymbol> fieldSymbols = new List <IFieldSymbol>(); foreach (FieldDeclarationSyntax field in syntaxReceiver.CandidateFields) { SemanticModel model = compilation.GetSemanticModel(field.SyntaxTree); foreach (VariableDeclaratorSyntax variable in field.Declaration.Variables) { IFieldSymbol fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol; if (fieldSymbol.GetAttributes() .Any(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default))) { fieldSymbols.Add(fieldSymbol); } } } foreach (IGrouping <INamedTypeSymbol, IFieldSymbol> group in fieldSymbols.GroupBy(f => f.ContainingType)) { string classSource = ProcessClassPrintableAttribute(group.Key, group.ToList(), attributeSymbol, context); context.AddSource($"{group.Key.Name}.Printables.cs", SourceText.From(classSource, Encoding.UTF8)); } context.AddSource("PrintableFields.PrintableAttribute", SourceText.From(attributeText, Encoding.UTF8)); }
private void ProcessPrintAllFieldPartialMethods(GeneratorExecutionContext context, MySyntaxReceiver syntaxReceiver, Compilation compilation) { // TODO: Implement - look at GivenSpecificPartialMethods_WhenHasFields_ThenAddPrintAllFieldsMethod test }