3

I'm using Visual Studio 2019 with SDK based project, with multiple targets:

<TargetFrameworks>netstandard2.0;net45;net46</TargetFrameworks>

enter image description here

But when I write conditional code, I see (obviously) some code in gray and some code in regular colors :

enter image description here

Question:

What settings decide which section will be gray and which will not? Because now, if I want to edit the "NETFULL" section (because I'm multitargeting), it's all gray and I don't have intellisense.

How can I tell VS: now let's switch to NETFULL mode?

NB Sure I can remove the condition but I want to know why is it gray and how can I switch between them ( since I'm multitargeting)

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

3 Answers3

4

To switch between the frameworks you are targeting use the dropdown at the top left of the code pane:

enter image description here enter image description here


Conditional-compilation symbols are declared in the .csproj file:

<PropertyGroup Condition="'$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net46'">
  <DefineConstants>NETFULL</DefineConstants>
</PropertyGroup>

or you can use the predefined symbols such as NETFRAMEWORK, NETSTANDARD or NETCOREAPP (or the versioned predefined symbols such as NETSTANDARD2_0).

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
0

It is the matter of conditional compilation symblols which you define in properites of your project.

Navigate to proprties tab of your project and select Build tab. There you can define those symobls.

enter image description here

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • But (regardless to define) - how does it know which section to show ? this dll can be called from net core or net fw. BTW , now I see that I missed the `defineConstant`section {search for "netfull" [here](https://weblog.west-wind.com/posts/2017/jun/22/multitargeting-and-porting-a-net-library-to-net-core-20))}. But even If I define those , how does it know the targetFramework ? it's only at runtime. becuase like i said , a console app ( dotnet core ) app can call this dll , and also 4.5 – Royi Namir Jun 08 '19 at 10:03
  • Well, you define your own configuration, or use debug configuration. Then just specify additional symbols, like I did in example. Then when you switch to defined configuration (in my case Debug (no hardware)), code under `#if Additional_Symbol` is active, gets compiled. – Michał Turczyn Jun 08 '19 at 10:06
-1

Redefine TargetFrameworks. Use the same syntax, just put in the other targets.