How to make a line of code inactive depending on platform?

谁说胖子不能爱 提交于 2019-12-11 12:03:19

问题


I have a challenge for you.

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.

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?


回答1:


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

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.

Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if




回答2:


I don't know a lot about ASP.NET, but can you make a build option named LOCALHOST or something similar that you set to true if you want it ran or not.

#if LOCALHOST
//do this line
#endif


来源:https://stackoverflow.com/questions/55539997/how-to-make-a-line-of-code-inactive-depending-on-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!