Say I have a class library that contains both .net2.0 compatible code, and .net5 compatible code. I want to be able to build the project for both frameworks, where the .net5 code is ignored when building the .net2.0 build. This way I can maintain only one version whilst catering for both frameworks.
Check the #define together with #if...
it set precompiler test and that's how you may achieve that... there may be other simpler options, but this one works mostly like in C++
This is an example from the link:
#define DEBUG
//#define TRACE
#undef TRACE
using System;
public class TestDefine
{
static void Main()
{
#if (DEBUG)
Console.WriteLine("Debugging is enabled.");
#endif
#if (TRACE)
Console.WriteLine("Tracing is enabled.");
#endif
}
}
// Output:
// Debugging is enabled.
so the idea will be to create #define NET5
and #define NET2
and test in the code for the defined one depending in what you are compiling for. that way the compiler will ignore one or the other...
#define NET5
#define NET2
#if (NET5)
NET5 code...
#endif
#if (NET2)
NET2 code...
#endif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.