为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2019-09-20 16:40:55
我有一些代码使用扩展方法,但使用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?
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

属性组只覆盖所以这会为击败你的设置DEBUG,TRACE或任何其他。- 请参阅MSBuild属性评估


此外,如果DefineConstants从命令行设置属性,则在项目文件中对其执行的操作无关紧要,因为该设置将成为全局只读。这意味着您对该值的更改将无提示失败。


维护现有定义常量的示例:


    <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v2.0' ">V2</CustomConstants>

    <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' ">V4</CustomConstants>

    <DefineConstants Condition=" '$(DefineConstants)' != '' And '$(CustomConstants)' != '' ">$(DefineConstants);</DefineConstants>

    <DefineConstants>$(DefineConstants)$(CustomConstants)</DefineConstants>

本节必须遵循任何其他定义的常量,因为这些常量不太可能以相加的方式设置


我只定义了那些2,因为这主要是我对我的项目感兴趣的,ymmv。


查看完整回答
反对 回复 2019-09-20
  • 3 回答
  • 0 关注
  • 419 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信