4

我的应用程序有几种配置来进行调试/发布构建以及 32 位和 64 位构建。现在,对于 32 位和 64 位构建,我需要引用不同的 dll(即那些使用 x86 构建的和那些使用 x64 构建的),但这些引用对于我的项目来说似乎是全局的,并且不依赖于配置。现在,当我从 32 位切换到 64 位构建(反之亦然)时,我总是必须交换引用。为不同配置实现不同引用的适当方法是什么?

4

3 回答 3

2

这可以通过对项目文件进行一点手动操作来完成。

首先,您需要右键单击项目,然后单击Unload Project。然后再次右键单击它并选择Edit [project name]

当它在编辑器中加载时,您将看到各种参考条目:

<ItemGroup>
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase">
        <RequiredTargetFramework>3.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="PresentationCore">
        <RequiredTargetFramework>3.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="PresentationFramework">
        <RequiredTargetFramework>3.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
        <SpecificVersion>False</SpecificVersion>
        <HintPath>..\Common\Lib\3rdParty\Prism\4.0\Desktop\Microsoft.Practices.ServiceLocation.dll</HintPath>
    </Reference>
</ItemGroup>

请注意,这些都在一个ItemGroup节点内。您现在可以执行一些魔术...向您的 ItemGroup 添加一个表达式,以便仅在构建配置为一定位数时使用它:

<ItemGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <!-- these are the references used when there is a Release x86 build -->
    <Reference Include="System.Xml" />
</ItemGroup>

请注意,无法通过 UI 执行此操作,因此您必须手动管理这些参考列表(例如,如果您需要添加另一个参考)。

另请注意,这不是黑客......它只是利用MSBuild 的一个功能(VS 使用它来构建您的项目)。您可以使用任何您喜欢的表达式来拥有任意数量的这些ItemGroup列表 - 如果它没有表达式,那么它将始终包含在构建中。

于 2013-03-04T11:11:42.130 回答
1

您可以使 csproj 的任何部分以配置和/或平台为条件,因此您可以将引用放在单独的部分中。请注意,我认为即使没有更改,每次都会强制重建,因为 VS 无法确定是否需要重建。可能不是问题,但它会增加编译时间。

例如

<ItemGroup Condition=" '$(Platform)' == 'x86' " >
    <Reference ...86bit DLL... >
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x64' " >
    <Reference ...64bit DLL... >
</ItemGroup>

我认为如果签名中没有任何内容在它们之间发生任何变化,但我不记得了,我认为您也可以将参考的提示路径设为有条件的。

于 2013-03-04T11:05:58.603 回答
0

考虑使用运行时程序集绑定

http://msdn.microsoft.com/en-us/library/twy1dw1e.aspx

这将用基于设置的绑定中定义的程序集替换您的程序集。

于 2013-03-04T11:14:56.387 回答