Search code examples
c#.net-corepreprocessor-directive

preprocessor directive in nuget library


I don't really understand those preprocessor directives what I have to write. I'm developing a library that should work for many frameworks e.g. .net framework 4.5, 4.6,... and for my application that runs with framework .NETStandard Version 1.5 -> so I guess this is dnxcore50?

public class MyClass
{
#if DOTNET5_4
    // do nothing
#else
    public void MyMethod()
    {
        Console.WriteLine("framework is supported");
        // and do anything with libraries available in the framework :)
    }
#endif
}

so this is what I got for now, but MyMethod is not available with any other framework. Using #if DOTNETCORE50 doesn't work, too.

I also tried to define constraints but my project fails to load when I try this.

Any idea what's the right solution?


Solution

  • There's no need to define them in project/solution-file anymore.

    So, I just post an answer matching the current state (a few are still missing like net47 and so on, but you know what I mean):

    #if (NET45 || NET451 || NET46 || NET461)
    #define NetFramework
    #endif
    
    #if (NETSTANDARD1_0 || NETSTANDARD2_0 || NETSTANDARD1_5 || NETSTANDARD1_3 || NETSTANDARD1_6 || NETCOREAPP1_0 || NETCOREAPP1_1 || NETCOREAPP2_0)
    #define NetCore
    #endif
    

    in what way they are now compatible or redundant, I dont' know. But this is the current names I know.

    then you can

    public class MyClass
    {
    
    #if NetFramework
        public void MyMethod()
        {
            Console.WriteLine("framework is supported");
            // and do anything with libraries available in the framework :)
        }
    #endif
    
    }