2

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?

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
  • Related : http://stackoverflow.com/questions/3436526/detect-target-framework-version-at-compile-time –  Jul 22 '16 at 19:08
  • @x... i tried this already but after changing my solution-file, the project always fails loading. – Matthias Burger Jul 22 '16 at 19:10
  • Not solution file, project file. You edit the wrong file. –  Jul 22 '16 at 23:49
  • The information you got, such as DNX and NETCORE50, is out of date. Everything changes when Mocrosoft ships .NET Core 1.0 last month, so you have to start all over again at http://dot.net . And the sad news is that things are changing again, if you check the latest roadmap at .NET Blog. – Lex Li Jul 23 '16 at 01:16
  • @x... I meant the projectfile. it finally works now :) thanks for ya help. – Matthias Burger Jul 25 '16 at 05:55
  • @Lex Li it really starts annoying me.. Since the first version of dnx on VS2015 so many things changed, and you always have to start again if you did something and there's a new version. And .Net-classes you really could need for development won't be available... the idea was good. but for now it's really just the idea. – Matthias Burger Jul 25 '16 at 05:55
  • 1
    @MatthiasBurger it is the very first time for Microsoft to develop a brand new platform in the wide open way, so we can observe the twists and turns (used to be their internal chats). So I think such changes are in all not a bad things, as it leads to more awareness (though confusion too). For most developers, they should wait till this fall's Visual Studio release, which completes the tooling, and fills the gaps. – Lex Li Jul 25 '16 at 06:57
  • @LexLi I think you're right and I'm looking forward to develop with this new platform. In my opinion this is still a big chaos.. I grew up with Java und Python :D the (for a new developer) perfect little worlds without problems on the side of the language. But sure, it is new, and I hope I won't have to switch anymore back to Java and can stay with C# :) I think that's going off topic - But was a nice discussion and thanks for sharing your opinion :) – Matthias Burger Jul 25 '16 at 07:33

1 Answers1

3

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

}
Matthias Burger
  • 5,549
  • 7
  • 49
  • 94