问题
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