本文介绍了我的代码如何在运行时检测它是否为 x86 或任何 CPU 编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多集成测试可以从文件中读取预期结果.我的测试通过相对路径访问这些文件.相对路径是 x86 与任何 CPU 不同的一级深度.例如,当我的测试在 x86 下运行时,他们需要读取以下文件../../TestResults/MyTest.csv",但在 Any CPU 下他们需要访问../TestResults/MyTest.csv"

I have a lot of integration tests which read expected results from files. My tests access those files by relative paths. Relative paths are one level of depth different for x86 vs Any CPU. For example, when my tests run under x86, they need to read the following file "../../TestResults/MyTest.csv", but under Any CPU they need to access "../TestResults/MyTest.csv"

到目前为止,我在每个测试装置中都有以下常量:

So far I have the following constant in every test fixture:

   private const string platformDependentPrefix = "";

如果我为 x86 运行测试,我需要在每个测试装置中手动将 "" 更改为 "../".

If I run my tests for x86, I need to manually change "" to "../" in every test fixture.

有没有办法让它自动化?

Is there a way to automate it?

推荐答案

非常笨拙但有效的方法:

Very hacky way but works:

public static string Platform
{
    get 
    {
        if (IntPtr.Size == 8)
            return "x64";
        else
            return "x86";
    }
}

您还可以访问 CSharpProjectConfigurationProperties3.PlatformTarget 属性.

Also you can access the CSharpProjectConfigurationProperties3.PlatformTarget property.

这篇关于我的代码如何在运行时检测它是否为 x86 或任何 CPU 编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:40