如何为visual studio项目添加和编译自定义'Platform'开关?

Vin*_*Vin 1 c# visual-studio-2008 visual-studio

我们希望在Visual Studio中为我们的项目提供两个自定义平台开关(配置管理器中的平台下拉列表).

例如,一个用于'桌面',一个用于'Web'.然后,目标构建任务基于平台开关以自定义方式编译代码.我们不想添加到Debug Release开关,因为我们需要为每个桌面和Web平台提供这些开关.

我们发现尝试这种方法的一种方法是修改.csproj文件以添加这样的东西

<Platform Condition=" '$(Platform)' == '' ">Desktop</Platform>
Run Code Online (Sandbox Code Playgroud)

并添加属性组,如,

    <PropertyGroup Condition=" '$(Platform)' == 'Web' ">
        <DefineConstants>/define Web</DefineConstants>
        <PlatformTarget>Web</PlatformTarget>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Platform)' == 'Desktop' ">
        <DefineConstants>/define Desktop</DefineConstants>
        <PlatformTarget>Desktop</PlatformTarget>
      </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

但是这仍然无效,编译器会抛出错误

/ platform的选项'Desktop'无效; 必须是anycpu,x86,Itanium或x64

那么它必须是其中一个选项,我们不能添加我们的自定义平台吗?

有没有人能够做到这一点?任何指针都会有所帮助.

更新:使用DebugDesktop和ReleaseDesktop将使用户更加复杂.因为"桌面"和"网络"实际上是平台,并且还有能力在下拉列表中添加新平台(即),我认为"平台"切换应该用于完全相同的目的.

ast*_*r.x 5

可能这个主题对于三年后的某个人来说会很有趣.我在配置构建平台和解决它们方面遇到了类似的困难.

您抛出的错误是因为PlatformTarget属性是使用Desctop设置的,而不是因为Platform属性.这两个属性有一些不同的含义.第一个真正最终指示构建过程的所有参与者应该使用哪个过程体系结构,而第二个允许在IDE内部定制构建环境.

在Visual Studio中创建项目时,ProcessTarget属性可以默认设置为PropertyGroups下的AnyCPU,它具有条件限制,例如" '... | $(Platform)'=='... | AnyCPU' ".但它并没有强制你做同样的事情.可以使用具有其他值的AnyCPU for Platform属性轻松设置ProcessTarget属性.

考虑到上述情况,您的示例可能如下所示:

<PropertyGroup Condition=" '$(Platform)' == 'Web' ">
    <DefineConstants>Web</DefineConstants>
    <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'Desktop' ">
    <DefineConstants>Desktop</DefineConstants>
    <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

它必须工作.

希望它对你有用.