visual studio 2010编译条件基于目标框架 [英] visual studio 2010 compiler conditions based on target framework
问题描述
我需要能够做到这一点在我的代码:
#如果NET_3_5
// .NET 3.5只有代码
的#else
//非.NET 3.5的代码
#ENDIF
根据这,我尝试添加这对我的csproj文件,但它并没有帮助:
< DefineConstants条件='$(TargetFrameworkVersion) '=='V3.5'> NET_3_5< / DefineConstants>
更新
下面是该项目的文件:
<?XML版本=1.0编码=UTF-8>?;
<项目ToolsVersion =4.0DefaultTargets =生成的xmlns =http://schemas.microsoft.com/developer/msbuild/2003>
<&的PropertyGroup GT;
<结构条件='$(配置)'==''>调试和LT; /结构>
<平台条件='$(平台)'==''>&值为anycpu LT; /平台和GT;
<&的ProductVersion GT; 8.0.30703< /&的ProductVersion GT;
< SchemaVersion> 2.0< / SchemaVersion>
< ProjectGuid> {FAA16900-38B9-4891-86C3-F595DA1FA6F7}< / ProjectGuid>
<输出类型>图书馆与LT; /输出类型>
< AppDesignerFolder>物业LT; / AppDesignerFolder>
< RootNamespace> RavenLinqpadDriver< / RootNamespace>
<&的AssemblyName GT; RavenLinqpadDriver< /的AssemblyName>
< TargetFrameworkVersion>&V3.5 LT; / TargetFrameworkVersion>
< FileAlignment> 512 LT; / FileAlignment>
< TargetFrameworkProfile />
< /&的PropertyGroup GT;
<的PropertyGroup条件='$(配置)| $(平台)'=='调试|值为anycpu'>
< DebugSymbols>真< / DebugSymbols>
<&DEBUGTYPE GT;全< / DEBUGTYPE>
<优化>假LT; /优化>
< OutputPath> bin\Debug\< / OutputPath>
< DefineConstants> DEBUG,TRACE< / DefineConstants>
< DefineConstants条件='$(TargetFrameworkVersion)'=='V3.5'> NET_3_5< / DefineConstants>
< ErrorReport>&提示LT; / ErrorReport>
将; WarningLevel→4&下; / WarningLevel>
< /&的PropertyGroup GT;
<的PropertyGroup条件='$(配置)| $(平台)'=='发布|值为anycpu'>
<&DEBUGTYPE GT; pdbonly< / DEBUGTYPE>
<优化>真< /优化>
< OutputPath> bin\Release\< / OutputPath>
< DefineConstants>跟踪和LT; / DefineConstants>
< ErrorReport>&提示LT; / ErrorReport>
将; WarningLevel→4&下; / WarningLevel>
< /&的PropertyGroup GT;
<&的PropertyGroup GT;
< SignAssembly>真< / SignAssembly>
< /&的PropertyGroup GT;
<&的PropertyGroup GT;
< AssemblyOriginatorKeyFile> RavenLinqpadDriverStrongNameKey.pfx< / AssemblyOriginatorKeyFile>
< /&的PropertyGroup GT;
<&的ItemGroup GT;
<参考包括=GalaSoft.MvvmLight>
< HintPath> .. \packages\MvvmLight.3.0.3\lib\\\
et35\GalaSoft.MvvmLight.dll< / HintPath>
< /参考和GT;
<参考包括=GalaSoft.MvvmLight.Extras>
< HintPath> .. \packages\MvvmLight.3.0.3\lib\\\
et35\GalaSoft.MvvmLight.Extras.dll< / HintPath>
< /参考和GT;
<参考包括=LINQPad>
< HintPath> .. \lib\LINQPad.exe< / HintPath>
<私人和GT假LT; /私人和GT;
< /参考和GT;
<参考包括=Newtonsoft.Json.Net35>
< HintPath> .. \lib\Newtonsoft.Json.Net35.dll< / HintPath>
< /参考和GT;
<参考包括=PresentationCore/>
<参考包括=PresentationFramework/>
<参考包括=Raven.Client.Lightweight-3.5>
< HintPath> .. \lib\Raven.Client.Lightweight-3.5.dll< / HintPath>
< /参考和GT;
<参考包括=系统/>
<参考包括=System.ComponentModel.DataAnnotations/>
<参考包括=System.Core程序/>
<参考包括=System.Windows.Interactivity,版本= 3.5.0.0,文化=中性公钥= 31bf3856ad364e35,的ProcessorArchitecture = MSIL>
< HintPath> .. \packages\MvvmLight.3.0.3\lib\\\
et35\System.Windows.Interactivity.dll< / HintPath>
< /参考和GT;
<参考包括=System.Xml.Linq的/>
<参考包括=System.Data.DataSetExtensions/>
<参考包括=System.Data这/>
<参考包括=的System.Xml/>
<参考包括=WindowsBase/>
< /&的ItemGroup GT;
<&的ItemGroup GT;
<编译包括=GuidValueConverter.cs/>
<编译包括=RavenDriver.cs/>
<编译包括=Properties\AssemblyInfo.cs/>
<编译包括=RavenConectionDialog.xaml.cs>
< DependentUpon> RavenConectionDialog.xaml< / DependentUpon>
< /编译>
<编译包括=RavenConnectionInfo.cs/>
<编译包括=RavenContext.cs/>
<编译包括=Utility.cs/>
< /&的ItemGroup GT;
<&的ItemGroup GT;
<无包括=packages.config/>
<无包括=RavenLinqpadDriverStrongNameKey.pfx/>
< /&的ItemGroup GT;
<&的ItemGroup GT;
<内容包括=header.xml>
< CopyToOutputDirectory>总是小于/ CopyToOutputDirectory>
< /内容及GT;
< /&的ItemGroup GT;
<&的ItemGroup GT;
<页面包含=RavenConectionDialog.xaml>
<亚型GT;&设计师LT; /亚型GT;
<发生器GT;的MSBuild:编译< /发电机>
< /页>
< /&的ItemGroup GT;
<导入项目=$(MSBuildToolsPath)\Microsoft.CSharp.targets/>
<&的PropertyGroup GT;
< PostBuildEvent>
< / PostBuildEvent>
< /&的PropertyGroup GT;
<! - 要修改你的构建过程中,添加你的任务内低于目标之一,并取消它。
其它类似的扩展点的存在,看到Microsoft.Common.targets。
<目标名称=BeforeBuild>
< /目标>
<目标名称=AfterBuild>
< /目标>
- >
< /项目>
看来,< ; DefineConstants>
元素只存在下的调试配置<的PropertyGroup>
,这意味着当你构建的释放,将不会生效。做最好的事情是添加你的财产在一个单独的无条件属性组。所以,闭幕后的第三个<的PropertyGroup>
项目中的元素中,添加以下内容:
<&的PropertyGroup GT;
< DefineConstants条件='$(TargetFrameworkVersion)'=='V3.5'> $(DefineConstants); NET_3_5< / DefineConstants>
< /&的PropertyGroup GT;
这将确保在你的新定义常量调试/释放常数被保留下来。
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:
<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v3.5' ">NET_3_5</DefineConstants>
update
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. 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>
<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.
这篇关于visual studio 2010编译条件基于目标框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!