[英]visual studio 2010 compiler conditions based on target framework
I need to be able to do this in my code: 我需要能够在我的代码中执行此操作:
#if NET_3_5
// .net 3.5 only code
#else
// non .net 3.5 code
#endif
Based on this , I tried adding this to my csproj file, but it didn't help: 基于此 ,我尝试将其添加到csproj文件中,但没有帮助:
<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">NET_3_5</DefineConstants>
Here's the project file: 这是项目文件:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{FAA16900-38B9-4891-86C3-F595DA1FA6F7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RavenLinqpadDriver</RootNamespace>
<AssemblyName>RavenLinqpadDriver</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">NET_3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>RavenLinqpadDriverStrongNameKey.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="GalaSoft.MvvmLight">
<HintPath>..\packages\MvvmLight.3.0.3\lib\net35\GalaSoft.MvvmLight.dll</HintPath>
</Reference>
<Reference Include="GalaSoft.MvvmLight.Extras">
<HintPath>..\packages\MvvmLight.3.0.3\lib\net35\GalaSoft.MvvmLight.Extras.dll</HintPath>
</Reference>
<Reference Include="LINQPad">
<HintPath>..\lib\LINQPad.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json.Net35">
<HintPath>..\lib\Newtonsoft.Json.Net35.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="Raven.Client.Lightweight-3.5">
<HintPath>..\lib\Raven.Client.Lightweight-3.5.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Interactivity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLight.3.0.3\lib\net35\System.Windows.Interactivity.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="GuidValueConverter.cs" />
<Compile Include="RavenDriver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RavenConectionDialog.xaml.cs">
<DependentUpon>RavenConectionDialog.xaml</DependentUpon>
</Compile>
<Compile Include="RavenConnectionInfo.cs" />
<Compile Include="RavenContext.cs" />
<Compile Include="Utility.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="RavenLinqpadDriverStrongNameKey.pfx" />
</ItemGroup>
<ItemGroup>
<Content Include="header.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Page Include="RavenConectionDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
It appears that the <DefineConstants>
element exists only under the Debug configuration <PropertyGroup>
, which means that it would not take effect when you are building release. 看来
<DefineConstants>
元素仅在Debug配置<PropertyGroup>
下存在,这意味着在构建发行版时它不会生效。 The best thing to do would be to add your property in a separate unconditional property group. 最好的办法是将您的财产添加到一个单独的无条件财产组中。 So, after the closing of the third
<PropertyGroup>
element in your project, add the following: 因此,在项目中的第三个
<PropertyGroup>
元素关闭之后,添加以下内容:
<PropertyGroup>
<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">$(DefineConstants);NET_3_5</DefineConstants>
</PropertyGroup>
This will ensure that the debug/release constants are preserved while your new constant is defined. 这将确保在定义新常量时保留调试/释放常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.