在编译时检测目标框架版本

我有一些代码使用扩展方法,但使用VS2008中的编译器在.NET 2.0下编译。为了促进这一点,我不得不声明ExtensionAttribute:


/// <summary>

/// ExtensionAttribute is required to define extension methods under .NET 2.0

/// </summary>

public sealed class ExtensionAttribute : Attribute

{

}

但是,我现在想要包含该类的库也可以在.NET 3.0,3.5和4.0下编译 - 没有'ExtensionAttribute在多个地方定义'警告。


当目标框架版本是.NET 2时,是否有任何编译时指令可用于仅包含ExtensionAttribute?


慕村225694
浏览 417回答 3
3回答

互换的青春

属性组只覆盖所以这会为击败你的设置DEBUG,TRACE或任何其他。- 请参阅MSBuild属性评估此外,如果DefineConstants从命令行设置属性,则在项目文件中对其执行的操作无关紧要,因为该设置将成为全局只读。这意味着您对该值的更改将无提示失败。维护现有定义常量的示例:&nbsp; &nbsp; <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v2.0' ">V2</CustomConstants>&nbsp; &nbsp; <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' ">V4</CustomConstants>&nbsp; &nbsp; <DefineConstants Condition=" '$(DefineConstants)' != '' And '$(CustomConstants)' != '' ">$(DefineConstants);</DefineConstants>&nbsp; &nbsp; <DefineConstants>$(DefineConstants)$(CustomConstants)</DefineConstants>本节必须遵循任何其他定义的常量,因为这些常量不太可能以相加的方式设置我只定义了那些2,因为这主要是我对我的项目感兴趣的,ymmv。
打开App,查看更多内容
随时随地看视频慕课网APP