"설정"된 경우에만 실행되어야하는 "디버그 전용"코드
디버깅하는 사람이 요청하는 경우에만 실행되는 일부 C # "디버그 전용"코드를 추가하고 싶습니다. C ++에서는 다음과 비슷한 작업을 수행했습니다.
void foo()
{
// ...
#ifdef DEBUG
static bool s_bDoDebugOnlyCode = false;
if (s_bDoDebugOnlyCode)
{
// Debug only code here gets executed when the person debugging
// manually sets the bool above to true. It then stays for the rest
// of the session until they set it to false.
}
#endif
// ...
}
로컬 정적이 없기 때문에 C #에서 똑같이 할 수는 없습니다.
질문 : C #에서이 작업을 수행하는 가장 좋은 방법은 무엇입니까?
- C # 전 처리기 지시문 (# if / # endif DEBUG)과 함께 개인 클래스 정적 필드를 사용해야합니까?
- Conditional 특성 (코드를 보유하기 위해)을 사용하고 프라이빗 클래스 정적 필드 ( C # 전 처리기 지시문 # if / # endif DEBUG로 둘러싸 이지 않음)를 사용해야합니다.
- 다른 것?
인스턴스 변수는 아마도 원하는 것을 수행하는 방법 일 것입니다. 프로그램의 수명 (또는 정적 메모리 모델에 따라 스레드) 동안 동일한 값을 유지하도록 정적으로 만들거나 객체 인스턴스의 수명 동안 제어하기 위해 일반 인스턴스 var로 만들 수 있습니다. 해당 인스턴스가 싱글 톤이면 동일한 방식으로 작동합니다.
#if DEBUG
private /*static*/ bool s_bDoDebugOnlyCode = false;
#endif
void foo()
{
// ...
#if DEBUG
if (s_bDoDebugOnlyCode)
{
// Code here gets executed only when compiled with the DEBUG constant,
// and when the person debugging manually sets the bool above to true.
// It then stays for the rest of the session until they set it to false.
}
#endif
// ...
}
간단히 말해서, pragma (전 처리기 지시문)는 프로그램 흐름을 제어하는 데 사용하는 약간의 까다로운 것으로 간주됩니다. .NET에는 "Conditional"속성을 사용하여이 문제의 절반에 대한 기본 제공 답변이 있습니다.
private /*static*/ bool doDebugOnlyCode = false;
[Conditional("DEBUG")]
void foo()
{
// ...
if (doDebugOnlyCode)
{
// Code here gets executed only when compiled with the DEBUG constant,
// and when the person debugging manually sets the bool above to true.
// It then stays for the rest of the session until they set it to false.
}
// ...
}
No pragmas, much cleaner. The downside is that Conditional can only be applied to methods, so you'll have to deal with a boolean variable that doesn't do anything in a release build. As the variable exists solely to be toggled from the VS execution host, and in a release build its value doesn't matter, it's pretty harmless.
What you're looking for is
[ConditionalAttribute("DEBUG")]
attribute.
If you for instance write a method like :
[ConditionalAttribute("DEBUG")]
public static void MyLovelyDebugInfoMethod(string message)
{
Console.WriteLine("This message was brought to you by your debugger : ");
Console.WriteLine(message);
}
any call you make to this method inside your own code will only be executed in debug mode. If you build your project in release mode, even call to the "MyLovelyDebugInfoMethod" will be ignored and dumped out of your binary.
Oh and one more thing if you're trying to determine whether or not your code is currently being debugged at the execution moment, it is also possible to check if the current process is hooked by a JIT. But this is all together another case. Post a comment if this is what you2re trying to do.
You could try this if you only need the code to run when you have a debugger attached to the process.
if (Debugger.IsAttached)
{
// do some stuff here
}
If you want to know whether if debugging, everywhere in program. Use this.
Declare global variable.
bool isDebug=false;
Create function for checking debug mode
[ConditionalAttribute("DEBUG")]
public static void isDebugging()
{
isDebug = true;
}
In the initialize method call the function
isDebugging();
Now in the entire program. You can check for debugging and do the operations. Hope this Helps!
I think it may be worth mentioning that [ConditionalAttribute]
is in the System.Diagnostics;
namespace. I stumbled a bit when I got:
Error 2 The type or namespace name 'ConditionalAttribute' could not be found (are you missing a using directive or an assembly reference?)
after using it for the first time (I thought it would have been in System
).
참고URL : https://stackoverflow.com/questions/5080477/debug-only-code-that-should-run-only-when-turned-on
'developer tip' 카테고리의 다른 글
gem 설치 권한 문제 (0) | 2020.09.23 |
---|---|
PHP에서 변수로 명명 된 객체 속성에 어떻게 액세스 할 수 있습니까? (0) | 2020.09.23 |
Javascript의 값으로 연관 배열을 정렬하는 방법은 무엇입니까? (0) | 2020.09.23 |
포토샵은 두 이미지를 어떻게 혼합합니까? (0) | 2020.09.23 |
대소 문자를 구분하지 않는 xpath contains () 가능? (0) | 2020.09.23 |