简体   繁体   English

将值分配给预处理器指令

[英]assign value to preprocessor directive

Either I did not understand preprocessor directives or they're not working. 我不了解预处理器指令,或者它们不起作用。

I'm trying to write an application for multiple frameworks. 我正在尝试为多个框架编写应用程序。 (DNX451, DNX46, NETSTANDARD1_5,...) (DNX451,DNX46,NETSTANDARD1_5,...)

So I always have to write something like (really simple example, I know I would not need it here) 因此,我总是必须编写类似的内容(非常简单的示例,我知道我在这里不需要它)

public class Test
{
#if !(NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP1_0)
    public int? testVar;
#else
    public int testVar;
#endif

    public string Method()
    {
#if !(NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP1_0)
        return (testVar ?? 0).ToString();
#else
        return testVar.ToString();
#endif
    }
}

so is there a possibility to define a variable? 那么有可能定义一个变量吗? At least per file, so I could say eg: 至少每个文件,所以我可以说例如:

#define NetCore (NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP1_0)

So I only have to write 所以我只需要写

public class Test
{
#if !NetCore
    public int? testVar;
#else
    public int testVar;
#endif
....

would be much less code and I could define it on top of my file. 代码会少得多,我可以在文件顶部定义它。

Or is this simply not possible with preprocessor derectives? 还是使用预处理器矫正器根本不可能做到这一点?

This seems to work for me (has to be at top of file): 这似乎对我有用(必须放在文件顶部):

#if (NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP1_0)
#define NetCore
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM