AutoActions for Skinny controllers–first implementation
Now with the implementation.
First, I find all the fields declarations that had the Autonotify
if (syntaxNode is FieldDeclarationSyntax fieldDeclarationSyntax
&& fieldDeclarationSyntax.AttributeLists.Count > 0)
{
foreach(var al in fieldDeclarationSyntax.AttributeLists)
{
var att = al.Attributes;
foreach(var at in att)
{
var x = at.Name as IdentifierNameSyntax;
if(autoActions.Contains(x.Identifier.Text))
{
CandidateFields.Add(fieldDeclarationSyntax);
return;
}
}
}
}
Second , I must find all the methods ( without the constructor) for generating data:
if (!(context.SyntaxReceiver is SyntaxReceiverFields receiver))
return;
var compilation = context.Compilation;
var fieldSymbols = new List<IFieldSymbol>();
foreach (var field in receiver.CandidateFields)
{
SemanticModel model = compilation.GetSemanticModel(field.SyntaxTree);
foreach (var variable in field.Declaration.Variables)
{
var fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol;
var attr = fieldSymbol.GetAttributes();
if (attr.Any(ad => ad.AttributeClass.Name == autoActions))
{
fieldSymbols.Add(fieldSymbol);
}
}foreach (var group in fieldSymbols.GroupBy(f => f.ContainingType))
{
string classSource = ProcessClass(group.Key, group.ToArray(), context);
if (string.IsNullOrWhiteSpace(classSource))
continue;context.AddSource($”{group.Key.Name}_autogenerate.cs”, SourceText.From(classSource, Encoding.UTF8));
}
}
You can find the source code at https://github.com/ignatandrei/AOP_With_Roslyn/releases/tag/2020.11.23 .
Leave a Reply