AOP with Roslyn–part 2

I want to transform code by injecting some simple code, like “Console.WriteLine(“method”)

So this code:

 

using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
              var dt=DateTime.Now;
        }
     }
}

 

should be modified to this code:

 

using System;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" + "\"Program_Main_6\"" + @");//this is automatically added
            var dt = DateTime.Now;
        }
    }
}

 
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

 

public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {


            if (node.Body == null || node.Body.Statements.Count == 0)
                return base.VisitMethodDeclaration(node);
            var parent = node.Parent as ClassDeclarationSyntax;
            if (parent == null)
                return base.VisitMethodDeclaration(node);

            var nameMethod = node.Identifier.Text;
            var nameClass = parent.Identifier.Text;
            Console.WriteLine(nameMethod);
            node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node);
            var lineStart = node.GetLocation().GetLineSpan().StartLinePosition;
            string nameVariable = $"{nameClass}_{nameMethod}_{lineStart.Line}";
            var cmd = SyntaxFactory.ParseStatement($"Console.WriteLine(\"{nameVariable}\");//this is automatically added");

            var blockWithNewStatements = new SyntaxList<StatementSyntax>();
            
            for (int i = node.Body.Statements.Count - 1; i >= 0; i--)
            {
                var st = node.Body.Statements[i];
                blockWithNewStatements = blockWithNewStatements.Insert(0, st);
            }

            blockWithNewStatements = blockWithNewStatements.Insert(0, cmd);

            var newBlock = SyntaxFactory.Block(blockWithNewStatements);

            var newMethod = SyntaxFactory.MethodDeclaration
                (node.AttributeLists, node.Modifiers, node.ReturnType,
                node.ExplicitInterfaceSpecifier, node.Identifier, node.TypeParameterList,
                node.ParameterList, node.ConstraintClauses,
                newBlock,
                node.ExpressionBody, node.SemicolonToken);


            var newNode = node.ReplaceNode(node, newMethod);

            return base.VisitMethodDeclaration(newNode);
        }

 

As test, I have created a TestMethodRewriterSimple that verifies that.

        [TestMethod]
        public void TestMethodRewriterSimple()
        {

            var rc = new RewriteCode();
            rc.Code = @"
using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
              var dt=DateTime.Now;
        }
     }
}";
            var result= rc.RewriteCodeMethod();
            var newCode = @"
using System;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" + "\"Program_Main_6\"" + @");//this is automatically added
            var dt = DateTime.Now;
        }
    }
}";
            Assert.AreEqual(newCode.Replace(Environment.NewLine,""), newCode.Replace(Environment.NewLine, ""));
        }
    }

You can find the code on https://github.com/ignatandrei/AOP_With_Roslyn

Friday links 244

  1. The Quiet Crisis unfolding in Software Development — Medium
  2. Microsoft meets Open Source · Ted Neward’s Blog
  3. Your Software is Never Perfect
  4. Docker: Windows containers on Windows host – step by step in Stapp.space
  5. 5 Ways Remote Teams Can Create a Culture of Accountability
  6. How to Build a Search Page with Elasticsearch and .NET
  7. Sayed Ibrahim Hashimi – MSBuild, Web Deploy (MSDeploy), ASP.NET – How to publish one web project from a solution
  8. Building ClickOnce Applications from the Command Line
  9. 24 Data Science, R, Python, Excel, and Machine Learning Cheat Sheets – Data Science Central
  10. 4 easy steps to becoming a data scientist – Data Science Central
  11. Entity Framework Customizing Code First to an Existing Database
  12. 10 Ways Introverts Can Succeed at Networking
  13. Snoop, the WPF Spy Utility – Home
  14. Top 10: Cei mai buni comandanţi militari din istorie | Historia
  15. Introducing HyperDev – Joel on Software
  16. keyboardDrummer/SmartReactives: A .NET library that detects when an expression changes its value
  17. Using Windows IExpress To Package IT Tools | NlightU Blog
  18. The top 10 projects to try out with your Raspberry Pi 3 – TechRepublic
  19. Ways You Need To Tell The Browser How To Optimize | CSS-Tricks
  20. My surprisingly positive take on .Net Core’s current direction | The Shade Tree Developer
  21. 3 ways to keep your asp.net mvc controllers thin – JonHilton.Net
  22. Builder: C# · Ted Neward’s Blog
  23. Fluentassertions
  24. Steve Blank on the Tech Bubble: ‘VCs Won’t Admit They’re in a Ponzi Scheme’ | Inc.com
  25. Using LAST_VALUE – SQLServerCentral
  26. Simple Source Code for Generating ‘2 of 5 Interleaved’ Image Barcode – CodeProject

AOP with Roslyn

What I am interested in is to make a tool that logs in when the program comes in and out of a method.

It’s good for identifying problems in code and for logging.

What already exists on the market:

PostSharp – one of the best – see https://www.postsharp.net/alternatives
Cecil http://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/
Fody https://github.com/Fody
NConcern: https://github.com/Virtuoze/NConcern
What I do not like is that they are hard to configure. So, instead of writing a configurator, I’m going to write a Roslyn AOP so it’s easy to use POST build event in a CI scenario

I’ve inspired at https://github.com/KeenSoftwareHouse/SpaceEngineers – See https://github.com/KeenSoftwareHouse/SpaceEngineers/tree/master/Sources/VRage.Scripting. Do not compile in VS2017, but you can analyze the code …

The second source of inspiration was http://cezarywalenciuk.pl/blog/programing/post/roslyn-kompilator-net-rewrite-z-csharpsyntaxrewriter – in Polish, but you can see the code … (it gets complicated at the end)

My code will be on https://github.com/ignatandrei/AOP_With_Roslyn

Crossover tournament

This Saturday I have taken “Java, .NET, Ruby on Rails Chief Software Architects Hiring Tournament(https://www.eventbrite.com/e/bucharest-java-net-ruby-on-rails-chief-software-architects-hiring-tournament-tickets-38534409456 )”


It was an occasion for me to see how sharp are my (old ) programming skills and ( rather new , compare with programming) architecture skills.


For the record , I have finished the 4th from > 30 participants– that ‘s awesome for me.



Here are the results:

  1. Twin Strings 30 /30
  2. Technical Aptitude 46 /60
  3. Ascending Binary Sort 30 /30
  4. Architect Aptitude 30.86 /60
  5. Code Review 34.76 /60

Total Score: 171.62

 

Some considerations:


  • Technical aptitude was with ASP.NET WebForms questions – a no-no  for me
  • I solved the practical problems( 1 and 3 ) with a mix of .NET Generic Collection and a lot of Linq. I have also seen a Ruby solution similar with .NET Linq. Functional programming is awesome!
  • I do not agree with many question within Architect track – many were technology specific ( Amazon, Casandra, Qlik, oterhs)
  • Code review – here I have had a personal problem. I did not want to think deeply to all questions –   time is always a precious resource and I have had other things to do also.

All in all, a beautiful day and I am glad to have participated.