简体   繁体   中英

Select .NET Framework target

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://docs.microsoft.com/en-us/dotnet/standard/frameworks

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?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM