Generating additional code through a custom attribute
问题 I am still fairly new to C# and I have a question regarding attributes. Is it possible to write a custom attribute which generates additional code at compile time. For example: [Forever] public void MyMethod() { // Code } Turns into: public void MyMethod() { while (true) { // Code } } 回答1: Out of the box, no, this isn't something that can be done. Using PostSharp though, this can be achieved: http://www.sharpcrafters.com/aop.net/compiletime-weaving 回答2: Afterthought works similar to PostSharp