2

I have a solution which contains 3 projects (NetCoreWeb, NetFrameworkWeb, SharedLibrary)

  • NetCoreWeb: .Net Core 2.2
  • NetFrameworkWeb: Net Framework 4.5.2
  • SharedLibrary: NetCoreApp 2.2 & Net Framework 4.5.2

enter image description here

This is the source code in MessageService.cs which is in SharedLibrary project:

public class MessageService
    {
        public string GetMessage()
        {
#if NETFRAMEWORK
            return "Net Framework";
#elif NETSTANDARD
            return "Net Standard";
#else
            return "Not supported";
#endif
        }
    }

Every time, only the source code in #elif NETSTANDARD is active and available for debugging, even I set NetFrameworkWeb as the active project:

enter image description here

How can I make the source code in #if NETFRAMEWORK block to be active when I set NetFrameworkWeb as an active project, and set the rest to be active when #elif NETSTANDARD is set as active ?

Thank you,

Redplane
  • 2,971
  • 4
  • 30
  • 59

1 Answers1

0

The #if* statements are compile-time definitions. Since your library targets .NET Standard only (as far as i can tell from the screenshot and described behaviour), the source code is compiled with the NETSTANDARD constant.

This resulting binary is then used from your .NET Core and .NET Framework projects so the string value does not change because they are used from a different project.

If you absolutely need to implement different behaviours for different runtimes, you will need to set up your library as multi-targeting project instead of a .NET Standard project by changing the library's csproj file from

<TargetFramework>netstandard2.0</TargetFramework>

to

<TargetFrameworks>netcoreapp2.2;net452</TargetFrameworks>

(note the additional s in TargetFrameworks)

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217