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/
Attributes (C#)
Learn how to use attributes to associate metadata or declarative information with code in C#. An attribute can be queried at run time by using reflection.
docs.microsoft.com
docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/attributes/caller-information
C# 예약된 특성: 호출자 정보 추적
이 특성은 멤버를 호출하는 코드에 대한 정보를 생성하도록 컴파일러에 지시합니다. CallerFilePath, CallerLineNumber 및 CallerMemberName을 사용하여 자세한 추적 정보를 제공합니다.
docs.microsoft.com
docs.microsoft.com/ko-kr/dotnet/api/system.attributetargets?view=net-5.0
AttributeTargets 열거형 (System)
특성을 적용하는 데 유효한 애플리케이션 요소를 지정합니다.Specifies the application elements on which it is valid to apply an attribute.
docs.microsoft.com
반응형
'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 |