基于常量的条件编译? [英] Conditional compilation based on constants?

查看:40
本文介绍了基于常量的条件编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以基于类中定义的某个常量值有条件地编译类和方法,例如,我想拥有类似的东西.

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
}

很显然,我知道通常在C#中如何进行条件编译,我想基于类中的常量值而不是基于预处理器定义进行条件编译.知道很多编译时的东西都可以使用常量(例如Guid属性),是否有一种方法可以基于类常量有条件地编译方法/类?有一个Conditional属性,但似乎它也只适用于define.

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.

我有一个具有多种构建模式的复杂项目,我想让其他人更轻松地构建我的项目.例如,我需要在一个设置上具有3种不同的模式(可能更多),而在另一种设置上具有2-3种不同的模式.因此,为了使其他人更容易地编译我的项目,我将需要具有3 * 3 = 9个不同的预处理器定义和9个不同的csproj文件.如果我需要添加更多设置/模式/常量,那么这个故事并不能真正实现.对于将要编译我的代码但以前没有看过它的人来说,这可能会变得非常混乱.至少使用常量编译器将对它进行类型检查,因此将事情弄乱的可能性较小.

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.

推荐答案

否-

C#不对

C# does not evaluate expressions in the #if preprocessor directive - it only looks to see if the symbol is defined :

当C#编译器遇到 #if 指令,最后是 #endif 指令时,仅在指定符号的情况下,编译器才会在指令之间编译代码已定义.与C和C ++不同,您不能将数字值分配给符号.C#中的#if语句为布尔型,仅测试符号是否已定义.

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

(添加了重点)

所以做自己想做的正确方法是:

So the proper way to do what you want is to do:

public class SomeClass {
   #if BUILD_MODE_ONE
       public void SomeMethod() { /* Implementation one */ }
   #elif BUILD_MODE_TWO
       public void SomeMethod() { /* Implementation two */ }
   #elif BUILD_MODE_THREE
       public void SomeMethod() { /* Implementation three */ }
   #endif
}

可以在该代码文件中定义符号,也可以通过CSC.EXE的命令行参数来定义符号.

Where the symbol could either be defined in that code file or via command-line parameters to CSC.EXE.

这篇关于基于常量的条件编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆