예제 #1
0
        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);
        }
예제 #2
0
        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));
        }
예제 #3
0
 private void ProcessPrintAllFieldPartialMethods(GeneratorExecutionContext context, MySyntaxReceiver syntaxReceiver, Compilation compilation)
 {
     // TODO: Implement - look at GivenSpecificPartialMethods_WhenHasFields_ThenAddPrintAllFieldsMethod test
 }