0

I've an application in which I'd use both .net std 2.0 and .net4.6.1. I've declared it in my .csproj file which seems to work.

<PropertyGroup>
  <TargetFrameworks> net461;netstandard2.0 </TargetFrameworks>
</PropertyGroup>

I've used pre-processor directives in order to get the code to behave the way I wanted.

#if NET461
  //.net 4.6.1 code
#else
  //.net std 2.0 code
#endif

When I run my application, I always get into the .net std 2.0 case. How do I specify that a specific part in my code should target a given framework?

I've followed these instructions: https://learn.microsoft.com/en-us/dotnet/standard/frameworks

random
  • 487
  • 1
  • 9
  • 19
  • Which framework are you using to compile your application? You can't compile it with two different framework targets at the same time. How exactly would that work? And what is the expected behaviour? – laptou Apr 27 '18 at 15:31
  • `#IF` conditionals are evaluated at compile time, not run time. – Ron Beyer Apr 27 '18 at 15:36
  • @333 Yes, you're right. It's being compiled by .net std. – random Apr 30 '18 at 08:20

1 Answers1

1

Here is how one of our projects is configured to use various versions of the .Net framework:

<startup useLegacyV2RuntimeActivationPolicy="false">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>

Edit:

Just open your XML body, add a <configuration> tag, and put the startup requirements into it.

Here is a complete (and basic) app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="false">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

useLegacyV2RuntimeActivationPolicy is explained in this question:

What does 'useLegacyV2RuntimeActivationPolicy' do in the .NET 4 config?

  • Thanks! I'm writing a Xamarin cross platform.These kind of projects doesn't seem to have an App.config file. Where should I specify the supportedRunTime information? – random Apr 30 '18 at 08:18
  • I thought you might ask that. I updated the answer. Hope that helps. –  Apr 30 '18 at 13:27