How to modify code before compilation?

前端 未结 3 912
一整个雨季
一整个雨季 2021-01-30 05:22

Using Roslyn, I would like to modify my C# code before the actual compilation. For now, I will just need something like:

[MyAnotatedMethod]
public void MyMethod(         


        
3条回答
  •  灰色年华
    2021-01-30 05:38

    It is a little more dirty than using an Annotation, but blocking code out using Preprocessor Directives will allow you to configure how your code compiles based on flags.

    http://www.codeproject.com/Articles/304175/Preprocessor-Directives-in-Csharp

    Code in the #ifMyFlag in the below will only be compiled if MyFlag is defined

    #define MyFlag
    public void MyMethod() 
    {
        #if MyFlag
            // Flag conditional code
    
        #endif
    
        // method-body 
    }
    

提交回复
热议问题