如何根据平台使一行代码无效? [英] How to make a line of code inactive depending on platform?

查看:142
本文介绍了如何根据平台使一行代码无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对你有挑战.

我有一个使用Microsoft Visual Studio 2017 C#内置的ASP.NET Web应用程序.为了将其附加到正在运行的进程进行调试,我们必须包含一行代码才能使其在本地主机上运行在网络浏览器中.

I have an ASP.NET Web Application built in C# with Microsoft Visual Studio 2017. There is a line of code that we have had to include in order for it to run on a localhost when debugging by attaching it to a running process in a web browser.

这是问题所在.当该进程在服务器上运行时,该行代码不是必需的.

Here is the issue. That line of code is not necessary when the process is running on the server.

当我在TFS中提交代码时,可以删除该行.但是,如果我们能够使代码根据运行在哪个平台上而以某种方式忽略该行,那就太好了.

I can remove that line when I submit the code in TFS. But it would be great if we could make the code somehow ignore that line depending on what platform it is running on.

您怎么认为可以做到这一点?

How can you think this could be done?

推荐答案

我过去在C ++中使用#if指令进行了此操作.一个快速的谷歌,这也可以在C#中使用.

I've done this in C++ in the past using the #if directive. A quick google and this is also available in C#.

通过定义#if CONDITION后跟#endif,包装在其中的代码仅在针对所定义的条件进行编译时才运行.在您的情况下,您可以使用#if DEBUG,并且以下代码仅在调试模式下执行.

By defining #if CONDITION followed by a #endif, the code wrapped inside is only ran when compiled for the condition defined. In your case, you could have #if DEBUG and the code following would only execute in debug mode.

参考: https://docs.microsoft.com/zh-CN/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

这篇关于如何根据平台使一行代码无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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