public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(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);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Visual Studio Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
namespace System.Runtime.CompilerServices {
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerMemberNameAttribute : Attribute {
}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerFilePathAttribute : Attribute {
}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerLineNumberAttribute : Attribute {
}
}
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/
docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/attributes/caller-information
docs.microsoft.com/ko-kr/dotnet/api/system.attributetargets?view=net-5.0
반응형
'Programming > C/C++/C#' 카테고리의 다른 글
C# 공용 구조체(FieldOffset) (0) | 2023.10.19 |
---|---|
ConsoleAppTester (0) | 2023.10.17 |
정규 표현식 Regex (0) | 2021.12.31 |
C# Random(min, max) (0) | 2020.05.06 |
C# Dictionary enum key 사용 시 GC 확인 (0) | 2020.02.23 |
C# WPF 파일 다이얼로그(FileDialog) (0) | 2019.11.15 |
C# 파일 감시자(FileSystemWatcher) 예제 (0) | 2019.11.06 |
C# Xamarin Mac에 페어링 (0) | 2019.04.26 |