Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Compiling - Phasing out Part of code in C#

I'm working on a project, where I need a set of classes to be designed and used for one phase of the project.

In the subsequent phase, we will not be needing the set of classes and methods. These set of classes and methods will be used throughout the application and so if I add them as any other classes, I would need to manually remove them once they are not required.

Is there a way in C#, so that I can set an attribute on the class or places where the class is instantiated, to avoid that instantiation and method calls based on the value of the attribute.

Something like, setting

[Phase = 2]
BridgingComponent bridgeComponent = new BridgeComponent();

Any help appreciated on this front.

like image 628
Rajeshwaran S P Avatar asked Feb 28 '23 15:02

Rajeshwaran S P


1 Answers

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined.

#define FLAG_1
...
#if FLAG_1
    [Phase = 2]
    BridgingComponent bridgeComponent = new BridgeComponent();
#else
    [Phase = 2]
    BridgingComponent bridgeComponent;
#endif
like image 103
serhio Avatar answered Mar 02 '23 16:03

serhio