Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

C# - 디자인 타임(design-time)과 런타임(runtime)의 코드 실행 구분

대부분의 환경에서, 개발자가 작성한 코드라는 것은 런타임에만 실행하는 것이 일반적입니다. 왜냐하면, (제가 아는 한) "디자인 타임"에 코드 실행을 하는 것은 비주얼 스튜디오 개발 환경이 유일하기 때문입니다.

예를 들어, 비주얼 스튜디오에서 Winform으로 작업을 하면 다음과 같은 화면을 보게 될 것입니다.

vs_design_time_1.png

저렇게 작업을 하면 비주얼 스튜디오는 TextBox의 위치와 Text 속성에 들어가는 "test" 문자열 값을 설정하는 코드를 "Form1.Designer.cs" 파일에 다음과 같은 식으로 변환해 둡니다.

namespace WindowsFormsApp1
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "test";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(396, 240);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
    }
}

즉, 비주얼 스튜디오는 Form 디자인 창이 뜨면 위의 InitializeComponent 메서드를 호출해 Form 화면을 구성합니다. 바로 이것이, "디자인 타임에 코드를 실행"하는 것을 의미합니다. (물론 런타임 시에도 InitializeComponent가 호출됩니다.)

간단한 테스트를 위해 InitializeComponent 메서드에 다음과 같이 문법 오류를 발생시켜 보면,

private void InitializeComponent()
{
    asbfsdafvkasd
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    ...[생략]...
}

비주얼 스튜디오의 디자인 창은 이제 다음과 같은 오류 메시지와 함께 폼 디자인 기능을 제공하지 못합니다.

vs_design_time_2.png

결국, 오직 닷넷 개발자에게만 "디자인 타임"과 "런타임"의 용어가 의미를 가집니다.




자신의 코드가 특정 모드에서만 실행하도록 바꾸고 싶다면 Component.DesignMode, DesignerProperties.GetIsInDesignMode 등의 방법을 통해,

Component.DesignMode Property
; https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.component.designmode?view=net-5.0

DesignerProperties.GetIsInDesignMode
; https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.designerproperties.getisindesignmode

실행 코드를 구분 지을 수 있습니다. 가령, 아래의 글은 WPF 환경에서의 예를 들고 있습니다.

WPF - XAML 로딩 시 Visual Studio 비정상 종료
; https://www.sysnet.pe.kr/2/0/602

이외에, 비주얼 스튜디오가 디자인 타임과의 연동을 위해 제공하는 기능을 알고 싶다면 다음의 글을 참고하시면 됩니다.

Building Windows Forms Controls and Components with Rich Design-Time Features
; https://docs.microsoft.com/en-us/archive/msdn-magazine/2003/april/design-time-features-for-windows-forms-controls-and-components

직접 사용해 본 적이 없어서 100% 단정 지을 수는 없지만 아마도 Rider도 폼 디자인 처리를 마이크로소프트가 정해 놓은 규칙을 따르는 듯합니다.

Developing Windows Forms Applications in Rider 2019.1
; https://blog.jetbrains.com/dotnet/2019/04/26/developing-windows-forms-applications-rider-2019-1/




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/5/2021]

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

비밀번호

댓글 작성자
 




... 16  17  18  19  20  21  22  23  [24]  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13022정성태3/31/20226548Windows: 202. 윈도우 11 업그레이드 - "PC Health Check"를 통과했지만 여전히 업그레이드가 안 되는 경우 해결책
13021정성태3/31/20226705Windows: 201. Windows - INF 파일을 이용한 장치 제거 방법
13020정성태3/30/20226440.NET Framework: 1187. RDP 접속 시 WPF UserControl의 Unloaded 이벤트 발생파일 다운로드1
13019정성태3/30/20226427.NET Framework: 1186. Win32 Message를 Code로부터 메시지 이름 자체를 구하고 싶다면?파일 다운로드1
13018정성태3/29/20226951.NET Framework: 1185. C# - Unsafe.AsPointer가 반환한 포인터는 pinning 상태일까요? [5]
13017정성태3/28/20226793.NET Framework: 1184. C# - GC Heap에 위치한 참조 개체의 주소를 알아내는 방법 - 두 번째 이야기 [3]
13016정성태3/27/20227644.NET Framework: 1183. C# 11에 추가된 ref 필드의 (우회) 구현 방법파일 다운로드1
13015정성태3/26/20228986.NET Framework: 1182. C# 11 - ref struct에 ref 필드를 허용 [1]
13014정성태3/23/20227594VC++: 155. CComPtr/CComQIPtr과 Conformance mode 옵션의 충돌 [1]
13013정성태3/22/20225920개발 환경 구성: 641. WSL 우분투 인스턴스에 파이썬 2.7 개발 환경 구성하는 방법
13012정성태3/21/20225276오류 유형: 803. C# - Local '...' or its members cannot have their address taken and be used inside an anonymous method or lambda expression
13011정성태3/21/20226753오류 유형: 802. 윈도우 운영체제에서 웹캠 카메라 인식이 안 되는 경우
13010정성태3/21/20225686오류 유형: 801. Oracle.ManagedDataAccess.Core - GetTypes 호출 시 "Could not load file or assembly 'System.DirectoryServices.Protocols...'" 오류
13009정성태3/20/20227235개발 환경 구성: 640. docker - ibmcom/db2 컨테이너 실행
13008정성태3/19/20226530VS.NET IDE: 176. 비주얼 스튜디오 - 솔루션 탐색기에서 프로젝트를 선택할 때 csproj 파일이 열리지 않도록 만드는 방법
13007정성태3/18/20226154.NET Framework: 1181. C# - Oracle.ManagedDataAccess의 Pool 및 그것의 연결 개체 수를 알아내는 방법파일 다운로드1
13006정성태3/17/20227189.NET Framework: 1180. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 remuxing.c 예제 포팅
13005정성태3/17/20226066오류 유형: 800. C# - System.InvalidOperationException: Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.
13004정성태3/16/20226090디버깅 기술: 182. windbg - 닷넷 메모리 덤프에서 AppDomain에 걸친 정적(static) 필드 값을 조사하는 방법
13003정성태3/15/20226238.NET Framework: 1179. C# - (.NET Framework를 위한) Oracle.ManagedDataAccess 패키지의 성능 카운터 설정 방법
13002정성태3/14/20226988.NET Framework: 1178. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 http_multiclient.c 예제 포팅
13001정성태3/13/20227395.NET Framework: 1177. C# - 닷넷에서 허용하는 메서드의 매개변수와 호출 인자의 최대 수
13000정성태3/12/20226957.NET Framework: 1176. C# - Oracle.ManagedDataAccess.Core의 성능 카운터 설정 방법
12999정성태3/10/20226448.NET Framework: 1175. Visual Studio - 프로젝트 또는 솔루션의 Clean 작업 시 응용 프로그램에서 생성한 파일을 함께 삭제파일 다운로드1
12998정성태3/10/20226062.NET Framework: 1174. C# - ELEMENT_TYPE_FNPTR 유형의 사용 예
12997정성태3/10/202210449오류 유형: 799. Oracle.ManagedDataAccess - "ORA-01882: timezone region not found" 오류가 발생하는 이유
... 16  17  18  19  20  21  22  23  [24]  25  26  27  28  29  30  ...