Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,...)

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 e.g.:

#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?

like image 643
Matthias Burger Avatar asked Dec 02 '25 06:12

Matthias Burger


1 Answers

This seems to work for me (has to be at top of file):

#if (NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP1_0)
#define NetCore
#endif
like image 186
Quantic Avatar answered Dec 03 '25 19:12

Quantic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!