Category: roslyn
-
AutoActions for Skinny controllers-Templating-part1
In order to do templating,I have transform all text generation into classes Example: code.AppendLine($”{fieldName}.{ms.Name}({parametersCall});”); into class MethodDefinition { public string ClassName { get; set; } public string Name { get; set; } public Type ReturnType; public bool ReturnsVoid; //name,type public Dictionary<string,string> Parameters; } ( of course,this definition has changed multiple times…) Then I have added…
-
AutoActions for Skinny controllers–small customizations
Logging Roslyn Code Generator: For each program that you develop,it is important ( if not vital ) to see logging. In the Roslyn Analyzer / Code Generators,the diagnostics are logged with DiagnosticDescriptor and Diagnostic – think that you should see in the output console when compiling and this will become clear. More,the enum DiagnosticSeverity Enum…
-
AutoActions for Skinny controllers–deploying at NuGet
First time I was thinking that is enough to do dotnet pack to make a Nuget package to deploy on NUGET. It was not. I realized that the generator was not starting at all! And it was not in the project Dependencies=>Analyzers . Time to read more from https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md,I realized that I was wrong –…
-
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…
-
AutoActions for Skinny controllers–idea
How you generate actions in controllers ? Usually,you create a Business Logic class and then you expose via a controller in a WebAPI . And,if you are very careful,then you use the Skinny controllers concept ( read more about it at 3 ways to keep your asp.net mvc controllers thin (jonhilton.net) ). So this implies…
-
[Video] 5 Minutes Roslyn Code Generators
[Video] 5 Minutes Roslyn Code Generators
-
AOP with Roslyn–part 5 – getting line number right
There is a problem with inserting lines – the number of lines is not the same. What I mean by this? Look at this example The DateTime.Now is on line 8 When we insert lines before and after each method,the DateTime.Now is on line 9 This is usually not so important –…
-
AOP with Roslyn–part 4–custom code to the end of the method
A good AOP needs also custom code to the beginning and to the end of the method. So we need to add formatter for the last line. That means that the MetodRewriter constructor will look like this: More,we should put code at the beginning and at the end of the method And…
-
AOP with Roslyn–part 3–custom code at beginning of each method
Last time(http://msprogrammer.serviciipeweb.ro/2017/11/27/aop-with-roslynpart-2/) we have injected a new code into each method . However,this code was pretty much hardcoded into the MethodRewriter class – it was a simple Console.WriteLine(\”{nameClass}_{nameMethod}_{lineStartNumber}\”); Now we want to can customize this code at the will of the programmer. For this,I have modified classes RewriteCode and MethodRewriter to accept a parameter…
-
AOP with Roslyn–part 2
I want to transform code by injecting some simple code,like “Console.WriteLine(“method”) So this code: should be modified to this code: How I do the code: I derive from CSharpSyntaxRewriter and override the VisitMethodDeclaration . I will construct a new node with the Console.WriteLine statement inserted As test,I have created…