Programming/C/C++/C#
미리 예약된 attribute 로 코드 정보 받아오기
휘탱
2020. 4. 18. 12:09
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
반응형