简体   繁体   English

是否有一个C#等价的Java的注释处理工具(apt)?

[英]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. Java的apt工具允许您根据放在代码中的注释在编译时执行代码。 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. C#有一些属性,就像Java的注释一样,但尝试我似乎无法找到一个现成的工具(理想情况下是一个开源的工具),为C#提供与apt相同的功能。 Does such a tool exist? 这样的工具存在吗?

There are some. 有一些。 Such as: 如:

  1. Sprint.Net (Open source?) http://www.springframework.net/doc-latest/reference/html/aop.html Sprint.Net(开源?) http://www.springframework.net/doc-latest/reference/html/aop.html
  2. Postsharp (Costs Money) http://www.sharpcrafters.com/aop.net/custom-attributes Postsharp(Costs Money) http://www.sharpcrafters.com/aop.net/custom-attributes
  3. A tutorial (Open source) http://www.codeproject.com/KB/architecture/aopsimplestscenario.aspx 教程(开源) 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 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. 我之前很快就看过PostSharp,它看起来很整洁。 Sprint.Net is a port from the java Sprint library I believe. 我相信Sprint.Net是java Sprint库的一个端口。 There are also numerous code generation libraries out there that you could leverage by reflecting over your assembiles and performing your logic. 还有许多代码生成库,您可以通过反映组件和执行逻辑来利用它们。

The short answer is No. 最简洁的答案是不。

But if you are willing to do your own tool, take a look at Mono.Cecil for instance. 但是,如果您愿意使用自己的工具,请查看Mono.Cecil 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 (ie executes your Mono.Cecil based tool). 如果使用Visual Studio(或类似),您可以设置一个项目,以便它构建执行命令(即执行基于Mono.Cecil的工具)。

看看af fody,一个用于编织.net程序集的可扩展工具: https//github.com/Fody/Fody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM