如何从 C# 项目的构建事件访问 Visual Studio 解决方案级平台? [英] How can you access the Visual Studio solution level platform from a C# project's build event?

查看:36
本文介绍了如何从 C# 项目的构建事件访问 Visual Studio 解决方案级平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个大型 VS 2010 解决方案,主要是 C# 代码,但也有一些本机 DLL,各种 C# 项目都依赖(包括我们的单元测试 DLL).我们正在尝试同时支持 32 位和 64 位版本的库.因此,我们现在将本机 DLL 构建为 32 位和 64 位.问题是我们的许多 C# 项目都有构建后事件,这些事件将所需的本机 DLL 复制到项目的 TargetDir 中.现在我们有两个不同版本的本机 DLL(32 位和 64 位),我需要能够指定正确的目录来从中复制本机 DLL.我原本以为我可以像这样在路径中简单地使用 $(Platform) :

We have a large VS 2010 solution that is mostly C# code but there are a few native DLLs that various C# projects depend upon (including our unit testing DLL). We're in the processing of trying to support both 32-bit and 64-bit versions of our libraries. So we're now build the native DLLs as 32-bit and 64-bit. The problem is that a lot of our C# project's have post-build events that copy the required native DLLs into the project's TargetDir. Now that we have two different versions of the native DLLs (32 and 64 bit), I need to be able to specify the correct dir to copy the native DLL from. I originally thought I could simply use $(Platform) in the path like so:

copy $(SolutionDir)NativeDll$(Platform)$(Configuration) $(TargetDir)

但这行不通,因为 $(Platform) 是项目的平台,而不是解决方案级平台.在这种情况下,$(Platform) 是任何 CPU".从我看到的 C# 项目中的构建后事件宏来看,似乎没有一种方法可以访问正在构建的解决方案级平台.有没有更好的方法来实现我的目标?

But that doesn't work because $(Platform) is the project's platform and not the solution level platform. In this case $(Platform) is "Any CPU". From what I can see looking at the post-build event macros in C# project, there doesn't appear to be a way to access the solution level platform that is being built. Is there a better way to accomplish my goal?

推荐答案

我相信解决方案的平台,与项目的平台不同,只是文本.

I believe the solution's platform, unlike that of the projects is simply text.

我过去的缺点是:

  1. 从解决方案中删除 Win32 和混合平台"(并在添加项目后继续这样做).

  1. Delete Win32 and "mixed platform" from solution (and keep doing so after adding projects).

将所有 C# DLL 设置为在解决方案平台 AnyCPU、x86、x64 中构建为 AnyCPU.(如果您希望能够在 Blend 中打开或者如果您在解决方案中有任何纯托管应用程序,请不要删除 AnyCPU.)

Set all C# DLLs to build as AnyCPU in solution platforms AnyCPU, x86, x64. (Do not delete AnyCPU if you want to be able to open in Blend or if you have any pure managed applications in the solution.)

将 C# EXE 和单元测试设置为在 x86 解决方案平台中的 x86 和 x64 解决方案平台中的 x64 中构建,而不是在 AnyCPU 解决方案平台中构建.

Set C# EXEs and unit tests to build in x86 in x86 solution platform and x64 in x64 solution platform and not to build at all in AnyCPU solution platform.

当解决方案是 x86 并输出到 $(ProjectDir)inx86$(Configuration) 时,将所有本机设置为在 Win32 中构建,并且与路径中的 obj 而不是 bin 中间相同.x64 与 x64 相同,而不是 x86 和 Win32.

Set all natives to build in Win32 when solution is x86 and output to $(ProjectDir)inx86$(Configuration) and intermediate to same with obj in path instead of bin. x64 the same with x64 instead of x86 and Win32.

设置 C# EXE 和单元测试的预构建事件,以从具有项目配置名称的相对路径复制它们所依赖的本机 DLL:$(Config)

Set pre build events of C# EXEs and unit tests to copy native DLLs they are depended on from relative path with project's configuration name: $(Config)

设置单元测试的类初始化以将测试 bin 目录(当然是正确的平台和配置)的全部内容复制到测试的 out 目录.使用 if #DEBUG 和 unsafe sizeof(IntPtr) 来告诉在哪里寻找测试的 bin 目录.

Set unit tests' class initialize to copy entire contents of tests bin dir (correct platform and configuration, of course) to tests' out dir. Use if #DEBUG and unsafe sizeof(IntPtr) to tell where to look for tests' bin dir.

从解决方案的部署位置手动(使用记事本)向使用 x86/x64 程序集的解决方案外部的 .csproj 文件添加相对引用路径,因此路径将包括 $(Platform) 和 $(Configuration) 并且不会是每个用户.

Manually (using Notepad) add relative reference path to .csproj files outside solution that use x86/x64 assemblies from solution's deployment location, so path will include $(Platform) and $(Configuration) and will not be per user.

微软:Visual Studio 中更好的 32/64 位支持将会真正到位.

Microsoft: Better 32/64 bit support in Visual Studio would be really in place.

这篇关于如何从 C# 项目的构建事件访问 Visual Studio 解决方案级平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆