Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

목록(List) 타입의 값을 디버깅 중 Watch 창에서 확인하는 방법

간단한 예로, 다음과 같은 코드를 디버거로 구동해 BreakPoint를 찍어 list를 watch 창에서 확인하면,

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>();

        for (int i = 0; i < 5; i ++)
        {
            list.Add(i); // 이 라인에 BreakPoint를 걸음.
        }
    }
}

다음과 같이 "Value" 칼럼에는 Count만 나옵니다.

list_debug_helper_1.png

값을 보기 위해 "list" 이름의 왼쪽에 있는 화살표를 누르면 다음과 같이 펼쳐지긴 하는데요.

list_debug_helper_2.png

문제는, F10/F11 키를 눌러 디버깅을 진행하면 항목이 추가될 때마다 펼쳐놓은 것이 접히게 되어 값을 확인하려면 다시 Watch 창에서 왼쪽의 펼침 마크를 눌러줘야 합니다. 디버깅하다보면 이 작업이 여간 귀찮지 않은데요. 그냥 차라리 Value 칼럼에 값을 보여주면 좋겠는데... 애석하게도 방법이 없습니다.

얼핏, 비주얼 스튜디오에서 제공하는 Visualizer 확장을 이용해 해결할 수 있을 듯 싶지만,

How to: Write a Visualizer
; https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/debugger/how-to-write-a-visualizer

Comprehensive list of Debugger Visualizers for Visual Studio
; http://alexpinsker.blogspot.kr/2009/06/comprehensive-list-of-debugger.html

아쉽게도 디버거 비주얼라이저는 반드시 창을 띄워서 해결해야 하고, 게다가 모달(modal) 형식으로 뜨기 때문에 F10/F11 키를 눌러 디버깅을 진행하려면 다시 창을 닫아야 하는 불편함이 있습니다. 다음은 이에 대한 개선 요청입니다. (저도 투표했습니다. ^^)

Make the debugging Visualizers non-modal windows
; http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5711364-make-the-debugging-visualizers-non-modal-windows




정식 해결책은 아니지만, 그래도 개인적으로 쓰고 있는 팁이 있다면 다음과 같은 식입니다. DEBUG 모드에서만 컴파일되도록 도우미 클래스를 하나 만들고,

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>();
#if DEBUG
        ListDebugHelper<int> listLDH = new ListDebugHelper<int>(list, ", ");
#endif

        for (int i = 0; i < 5; i++)
        {
            list.Add(i);
        }
    }
}

#if DEBUG
class ListDebugHelper<T>
{
    IEnumerable<T> _list;
    string _split = string.Empty;

    public ListDebugHelper(IEnumerable<T> list) : this(list, string.Empty)
    {
    }

    public ListDebugHelper(IEnumerable<T> list, string split)
    {
        _list = list;
        _split = split;
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in _list)
        {
            sb.Append(item);
            sb.Append(_split);
        }

        return sb.ToString();
    }
}
#endif

그것의 인스턴스를 Watch 창에 등록해 두면 다음과 같이 Value 칼럼을 통해 직접 확인할 수 있습니다.

list_debug_helper_3.png

혹시 개인적으로 사용하고 있는 더 나은 팁이 있으시다면 덧글 공유 부탁드립니다. ^^

(첨부한 파일은 위의 예제를 테스트한 코드입니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 1/26/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2023-01-26 09시06분
정성태

... 91  92  93  94  95  96  [97]  98  99  100  101  102  103  104  105  ...
NoWriterDateCnt.TitleFile(s)
11215정성태6/1/201713021오류 유형: 395. 관리 콘솔을 실행하면 "This app has been blocked for your protection" 오류 발생 [1]
11214정성태6/1/201711208오류 유형: 394. MSDTC 서비스 시작 시 -1073737712(0xC0001010) 오류와 함께 종료되는 문제 [1]
11213정성태5/26/201715064오류 유형: 393. TFS - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
11212정성태5/26/201715164오류 유형: 392. Windows Server 2016에 KB4019472 업데이트가 실패하는 경우
11211정성태5/26/201713937오류 유형: 391. BeginInvoke에 전달한 람다 함수에 CS1660 에러가 발생하는 경우
11210정성태5/25/201714331기타: 65. ActiveX 없는 전자 메일에 사용된 "개인정보 보호를 위해 암호화된 보안메일"의 암호화 방법
11209정성태5/25/201754757Windows: 143. Windows 10의 Recovery 파티션을 삭제 및 새로 생성하는 방법 [16]
11208정성태5/25/201720580오류 유형: 390. diskpart의 set id 명령어에서 "The specified type is not in the correct format." 오류 발생
11207정성태5/24/201720720Windows: 142. Windows 10의 복구 콘솔로 부팅하는 방법
11206정성태5/24/201714343오류 유형: 389. DISM.exe - The specified image in the specified wim is already mounted for read/write access.
11205정성태5/24/201714036.NET Framework: 658. C#의 tail call 구현은?
11204정성태5/22/201724036개발 환경 구성: 316. 간단하게 살펴보는 Docker for Windows [7]
11203정성태5/19/201713005오류 유형: 388. docker - Host does not exist: "default"
11202정성태5/19/201713218오류 유형: 387. WPF - There is no registered CultureInfo with the IetfLanguageTag 'ug'.
11201정성태5/16/201715620오류 유형: 386. WPF - .NET 3.5 이하에서 TextBox에 한글 입력 시 TextChanged 이벤트의 비정상 종료 문제 [1]파일 다운로드1
11200정성태5/16/201712586오류 유형: 385. WPF - 폰트가 없어 System.IO.FileNotFoundException 예외가 발생하는 경우
11199정성태5/16/201714640.NET Framework: 657. CultureInfo.GetCultures가 반환하는 값
11198정성태5/10/201715940.NET Framework: 656. Windows Forms의 오류(Exception) 처리 방법에 대한 차이점 설명
11197정성태5/8/201713114개발 환경 구성: 315. VHD 파일의 최소 크기파일 다운로드1
11196정성태5/4/201714275오류 유형: 384. Msvm_ImageManagementService WMI 객체를 사용할 때 오류 상황 정리 [1]
11195정성태5/3/201714481.NET Framework: 655. .NET Framework 4.7 릴리스
11194정성태5/3/201716567오류 유형: 383. net use 명령어로 네트워크 드라이브 연결 시 "System error 67 has occurred." 오류 발생
11193정성태5/3/201715441Windows: 141. 설치된 Windows로부터 설치 이미지를 만드는 방법
11192정성태5/2/201715198Windows: 140. unattended.xml/autounattend.xml 파일을 마련하는 방법
11191정성태5/2/201716203Windows: 139. Dell Venue 8 Pro 태블릿에 USB를 이용한 윈도우 운영체제 설치 방법
11190정성태5/2/201721162Windows: 138. Windows 운영체제의 ISO 설치 파일에 미리 Device driver를 준비하는 방법
... 91  92  93  94  95  96  [97]  98  99  100  101  102  103  104  105  ...