Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# equivalent of Java's Annotation Processing Tool (apt)?

Java's apt tool allows you to execute code at compile time based upon the annotations you place in your code. C# has attributes, which work like Java's annotations, but try as I might I can't seem to find a ready-made tool (ideally an open source one) that provides the same functionality for C# as apt does for Java. Does such a tool exist?

like image 304
swift Avatar asked Dec 07 '11 18:12

swift


3 Answers

There are some. Such as:

  1. Sprint.Net (Open source?) http://www.springframework.net/doc-latest/reference/html/aop.html
  2. Postsharp (Costs Money) http://www.sharpcrafters.com/aop.net/custom-attributes
  3. A tutorial (Open source) http://www.codeproject.com/KB/architecture/aopsimplestscenario.aspx

The google search I used was:

https://www.google.com/search?q=.net+aop+attributes&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

I have had a look at PostSharp quickly before and it looks very tidy. Sprint.Net is a port from the java Sprint library I believe. There are also numerous code generation libraries out there that you could leverage by reflecting over your assembiles and performing your logic.

like image 124
Dessus Avatar answered Oct 05 '22 23:10

Dessus


The short answer is No.

But if you are willing to do your own tool, take a look at Mono.Cecil for instance. It allows to inspect and modify an assembly. If are using Visual Studio (or similar) you can setup a project so that it executes commands after building (i.e. executes your Mono.Cecil based tool).

like image 25
metator Avatar answered Oct 06 '22 00:10

metator


take a look af fody, a extensible tool for weaving .net assemblies: https://github.com/Fody/Fody

like image 28
David Leitner Avatar answered Oct 06 '22 01:10

David Leitner