2009-03-17 2 views

답변

97
Boolean isDebugMode = false; 
#if DEBUG 
isDebugMode = true; 
#endif 

당신은 당신이 이런 식으로해야합니까 빌드 디버그 및 릴리스 사이의 다른 동작을 프로그래밍하려면 : 당신이 기능의 디버그 버전에서 특정 검사를 수행하려는 경우

#if DEBUG 
    int[] data = new int[] {1, 2, 3, 4}; 
#else 
    int[] data = GetInputData(); 
#endif 
    int sum = data[0]; 
    for (int i= 1; i < data.Length; i++) 
    { 
    sum += data[i]; 
    } 

또는 당신이 그것을 할 수 이렇게 :

public int Sum(int[] data) 
{ 
    Debug.Assert(data.Length > 0); 
    int sum = data[0]; 
    for (int i= 1; i < data.Length; i++) 
    { 
    sum += data[i]; 
    } 
    return sum; 
} 

Debug.Assert은 릴리스 빌드에 포함되지 않습니다.

+0

는 JIT 최적화 된 빌드에 대해 묻는 OP인가? 그렇다면이 대답은 정확하지 않습니다. Debug 속성은 JIT Optimized 빌드에서 선언되거나 최적화되지 않을 수 있습니다. –

11

나는이 당신을 위해 유용 희망 :

public static bool IsRelease(Assembly assembly) { 
    object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true); 
    if (attributes == null || attributes.Length == 0) 
     return true; 

    var d = (DebuggableAttribute)attributes[0]; 
    if ((d.DebuggingFlags & DebuggableAttribute.DebuggingModes.Default) == DebuggableAttribute.DebuggingModes.None) 
     return true; 

    return false; 
} 

public static bool IsDebug(Assembly assembly) { 
    object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true); 
    if (attributes == null || attributes.Length == 0) 
     return true; 

    var d = (DebuggableAttribute)attributes[0]; 
    if (d.IsJITTrackingEnabled) return true; 
    return false; 
} 
+4

두 함수에 다음 행이있는 이유 if (attributes == null || attributes.Length == 0) return true; 이 코드에 문제가 있습니다. 답을 +1 한 이유는 구문을 사용하여 플래그를 얻는 것이 아니라 실제 프로그래밍 방식을 제공하기 때문입니다. 때때로 디버그 모드에서 컴파일러 플래그가 아닌 코드 자체의 일부로 표현되는지 여부를 알아야 할 필요가 있습니다. –

+1

Release 모드로 컴파일하고 DebugOutput을 "none"이외의 것으로 선택하면 DebuggableAttribute가 나타납니다. 따라서이 대답은 정확하지 않습니다. JIT 최적화 플래그도 찾지 않습니다. 차이점을 직접 또는 프로그래밍 방식으로 알려주는 방법에 대한 내 게시물을 참조하십시오. http://dave-black.blogspot.com/2011/12/how-to-tell-if-assembly-is-debug-or.html –

+2

이 경우의 어려움을 @DaveB에 맡기십시오. 그러나 귀하의 질문은 광범위했고, 테스트 할 때 코드가 다르게 동작하기를 원한다면 VB.Net에서이 테스트를 유용하게 사용할 수 있습니다. If If System.Diagnostics.Debugger.IsAttached Then DoSomething '(Form Behave Differently와 같은)' –

관련 문제