메서드 호출시 메서드 안에서 어디에서 호출된 것인지 알고 싶을때가 있다
특히 로그 남길 때 유용하게 쓸수 있다.
아래 코드 처럼 메서드 인자로 [CallerMemberName], [CallerFilePath], [CallerLineNumber] 를 선언해 주면 알아서 내용이 들어가게된다.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace CompilerServicesTest
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
            CallMethod();
        }
        private void CallMethod()
        {
            WriteLog("CompilerServices Test!!");
        }
        private void WriteLog(string message,
           [CallerMemberName] string memberName = "",
           [CallerFilePath]   string sourceFilePath = "",
           [CallerLineNumber] int sourceLineNumber = 0)
        {
            Trace.WriteLine("message: " + message);
            Trace.WriteLine("member name: " + memberName);
            Trace.WriteLine("source file path: " + sourceFilePath);
            Trace.WriteLine("source line number: " + sourceLineNumber);
        }
    }
}
참고
https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/attributes/caller-information
'C# > Winform' 카테고리의 다른 글
| [C#] 파일을 선택하여 windows 탐색기 열기 (0) | 2020.05.27 | 
|---|---|
| [C#] 캡쳐 방지하기 (0) | 2020.05.21 | 
| [C#] 자신의 컴퓨터에 설치된 Font 목록 나열하기 (0) | 2020.02.24 | 
| IIS 에 ASP.NET 등록방법 (0) | 2020.02.07 | 
| 간단한 Web API 만들기 (0) | 2020.02.07 | 





