#if DEBUG 指令在已编译的 class 库中
#if DEBUG directive in compiled class library
我将在 class 库中使用 #if DEBUG 指令。编译后的 ClassLibrary.dll 将在单独的 Application.exe.
中使用
我在这个 class 库中的调试代码会在以下情况下执行吗?
- 库在 DEBUG 模式下编译,应用程序在 RELEASE 下编译
- 库在 RELEASE 中编译,应用程序在 DEBUG 中编译
编译器指令在编译时解释,而不是在运行时解释。因此,如果使用的应用程序是在 RELEASE 或 DEBUG 模式下编译的,这并不重要。因此
1.) 库在 DEBUG 模式下编译,应用程序在 RELEASE 下编译 => 是
2.) 库在 RELEASE 中编译,应用程序在 DEBUG 中编译 => 否
我有一个类似的问题,尽管我在调试预处理器指令中配置了调试模式设置,但发布模式仍在选择调试模式设置。原来问题出在如何将 class 库项目的引用 dll 添加到我的项目中。有两种方法可以添加对项目的引用。
Option1: Add .dll from debug/release folder of the project to the
project that is dependent on it.
Problem: This introduces a problem, where in if you have added reference to the
debug version of the dll, even if you run the application in release
mode, its still going to refer to the debug version.
Fix: You will have to replace the .dll corresponding to what mode you want to run
in. Eg: If you are going to want to run in release mode, you will have
to replace the dependency dll with its corresponding release version.
Option2: Add the project reference (.csproj) file of the project you want to refer.
Advantage: This ensures when the whole solution is
built in release mode, it correctly resolves all dependencies to be in
release mode and when you build the solution in debug, it gets all
dependency projects in debug mode. This doesn't need your
intervention.
Where is option1 useful? -> When the projects your project wants to
reference don't exist in the same solution as yours.. Think of
scenarios where you want to use 3rd party dlls.. In this case you
either don't have the 3rd party code but got only the dlls or it lies
in a different solution.
When is option2 useful? -> When both
referencing project and referenced project exist in the same solution
(this is my scenario). If you remember, all my projects exist in the
same solution.
Link that explains how to add project references in 2 ways:
https://www.c-sharpcorner.com/article/project-reference-vs-dll-reference-in-visual-studio/
我将在 class 库中使用 #if DEBUG 指令。编译后的 ClassLibrary.dll 将在单独的 Application.exe.
中使用我在这个 class 库中的调试代码会在以下情况下执行吗?
- 库在 DEBUG 模式下编译,应用程序在 RELEASE 下编译
- 库在 RELEASE 中编译,应用程序在 DEBUG 中编译
编译器指令在编译时解释,而不是在运行时解释。因此,如果使用的应用程序是在 RELEASE 或 DEBUG 模式下编译的,这并不重要。因此
1.) 库在 DEBUG 模式下编译,应用程序在 RELEASE 下编译 => 是
2.) 库在 RELEASE 中编译,应用程序在 DEBUG 中编译 => 否
我有一个类似的问题,尽管我在调试预处理器指令中配置了调试模式设置,但发布模式仍在选择调试模式设置。原来问题出在如何将 class 库项目的引用 dll 添加到我的项目中。有两种方法可以添加对项目的引用。
Option1: Add .dll from debug/release folder of the project to the project that is dependent on it. Problem: This introduces a problem, where in if you have added reference to the debug version of the dll, even if you run the application in release mode, its still going to refer to the debug version.
Fix: You will have to replace the .dll corresponding to what mode you want to run in. Eg: If you are going to want to run in release mode, you will have to replace the dependency dll with its corresponding release version.Option2: Add the project reference (.csproj) file of the project you want to refer.
Advantage: This ensures when the whole solution is built in release mode, it correctly resolves all dependencies to be in release mode and when you build the solution in debug, it gets all dependency projects in debug mode. This doesn't need your intervention.Where is option1 useful? -> When the projects your project wants to reference don't exist in the same solution as yours.. Think of scenarios where you want to use 3rd party dlls.. In this case you either don't have the 3rd party code but got only the dlls or it lies in a different solution.
When is option2 useful? -> When both referencing project and referenced project exist in the same solution (this is my scenario). If you remember, all my projects exist in the same solution.
Link that explains how to add project references in 2 ways: https://www.c-sharpcorner.com/article/project-reference-vs-dll-reference-in-visual-studio/