简体   繁体   English

如何区分VS版本和csc.exe版本?

[英]How to distinguish between VS build and csc.exe build?

We have two ways to run tests on our product. 我们有两种方法可以对我们的产品进行测试。 One is local (using MsTest and VS) and the other is using a distant machine (let's call it M). 一种是本地的(使用MsTest和VS),另一种则使用的是远程计算机(我们称其为M)。 When uploading the code to M, M uses a batch script to generate configuration classes from xml files (which are kept in M) and builds the product using csc.exe. 将代码上传到M时,M使用批处理脚本从xml文件(保留在M中)生成配置类,并使用csc.exe构建产品。 When running tests from the local machine, the xml files aren't used, instead the configuration is taken from in memory classes. 从本地计算机运行测试时,不使用xml文件,而是从内存类中获取配置。 This means that we have to make changes to the code each time depending on where we want to run the tests - inherit from different configuration classes (in memory configuration class or external configuration class). 这意味着我们每次必须根据要在哪里运行测试来更改代码-继承自不同的配置类(在内存配置类或外部配置类中)。

Therefore, is there any way to know at run time if the SW was built using VS or a .cmd script? 因此,有什么方法可以在运行时知道SW是使用VS还是.cmd脚本构建的?

You can use a different build configuration from VS and from the script and so you can do something like this from your code: 您可以使用与VS和脚本不同的构建配置,因此可以从代码中执行以下操作:

public bool IsDebugBuild
{
    get
    {
    #if DEBUG
        return true;
    #else
        return false;
    #endif
    }
}

Just define your own configurations. 只需定义自己的配置即可。

I don't know of an "automatic" way to discern between the builds as the VS build also uses csc.exe internally. 我不知道一种“自动”的方式来区分内部版本,因​​为VS内部也在内部使用csc.exe。

But you could introduce a compile-time constant that you define only when building in the batch script, eg: 但是您可以引入一个仅在使用批处理脚本进行构建时才定义的编译时常量,例如:

internal static class BuildInfo
{
    internal static bool BuiltByBatchScript
    {
        get
        {
#if BATCH_BUILD
            return true;
#else
            return false;
#endif
        }
    }
}

When calling csc.exe in your batch script, add the following parameter: /define:BATCH_BUILD 在批处理脚本中调用csc.exe时,添加以下参数: /define:BATCH_BUILD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM