How to automatically generate boolean calls to your C # source code?

What I would like to generate in method bodies:

private void SomeMethod()
{
Logger.Log("Entering SomeMethod.");

// do stuff here

Logger.Log("Exiting SomeMethod.")
}

      

Is there a tool that can create this for me? If it doesn't, and I would like to implement it myself, then where to start, which library should I look for recognition methods in the C # source code? Should simple regular expressions be enough?

0


a source to share


4 answers


check postsharp

http://www.postsharp.org/



it is an aspect oriented framework. This will allow you to paint your methods with attributes that will trigger loggers, security checks, etc.

+5


a source


You're definitely looking for PostSharp here - Aspect Oriented Programming (AOP) for .NET. This will allow you to add handlers that will run before and after the arbitrary methods start, so you just need to define your logging code in one place.



+5


a source


Use the AOP structure for this.

Take a look at Spring.NET or Dynamic Proxy lock.

Code generation is usually not a good idea in most cases.

+1


a source


If you intend to generate code, you must actually perform a complete (and correct) analysis of each code file.

Some searches for "C Sharp Parser" give several possible results: Here And Here

Then you only need to go through the parse tree and add method calls at the beginning and end of each method.

+1


a source







All Articles