Is it possible to conditionally compile classes and methods based on some constant value defined in a class, for example I'd like to have something like that.
public enum BuildMode : int {
One = 1,
Two = 2,
Three = 3
}
public class BuildConfig {
// I will change this line, based on what build I want
public const BuildMode Mode = BuildMode.One;
}
public class SomeClass {
#if BuildConfig.Mode == BuildMode.One
public void SomeMethod() { /* Implementation one */ }
#elif BuildConfig.Mode == BuildMode.Two
public void SomeMethod() { /* Implementation two */ }
#elif BuildConfig.Mode == BuildMode.Three
public void SomeMethod() { /* Implementation three */ }
#endif
}
Obviously I know how condition compilation is done in C# in general, I want to make conditional compilation based on constant value in a class, not based on preprocessor define. Knowing that a lot of compile time stuff works with constants (like Guid attributes for example), is there a way to conditionally compile methods/classes based on class constants? There is a Conditional attribute, but it seems that it only works with defines as well.
I'm having a complex project with multiple build modes and I want to make it easier for other people to build my project. For example, I need to have like 3 different modes (potentially more) on one setting and like 2-3 different modes on the other setting. So to make it easier for other people to compile my project I will need to have like 3 * 3 = 9 different preprocessor defines and 9 different csproj files. And this story doesn't really scale if I will need add some more settings/modes/constants. And it can become really messy for a the person who will compile my code, but haven't seen it before. At least with the constants compiler will type check it, so there are less possibilities to mess things up.
Obviously I could build code to support all variants of configuration constants and call the code based on them at runtime, but I want to make optimization so that unused code won't be compiled in.