//调用
public static void UseCallerMemberNameAttribute()
{
	TraceMessage("Something happened.");
}

public static void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
	Console.WriteLine("message: " + message);
	Console.WriteLine("member name: " + memberName);
	Console.WriteLine("source file path: " + sourceFilePath);
	Console.WriteLine("source line number: " + sourceLineNumber);
}

输出

message: Something happened.
member name: UseCallerMemberNameAttribute
source file path: ...
source line number: 38

示例代码

ReflectionDemo

参考资料

Get the name of the currently executing method in dotnet core

相关文章:

  • 2021-07-10
  • 2022-02-24
  • 2022-12-23
  • 2021-11-14
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-19
  • 2021-07-24
  • 2021-06-19
  • 2021-09-22
相关资源
相似解决方案