CallerMemberName/CallerFilePathAttribute/CallerLineNumber 对性能的影响

Juh*_*älä 2 c# performance attributes annotations

CallerMemberName//CallerFilePathAttribute属性的使用如何CallerLineNumber影响应用程序的性能?这是在应用程序中编译的东西,还是相关反射的东西,还是其他东西?

我计划做类似的事情:

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

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

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute?view=net-7.0&redirectedfrom=MSDN

Gur*_*ron 5

该属性由编译器处理,即TraceMessage("Something happened.");编译器将替换为以下内容:

TraceMessage("Something happened.", "DoProcessing", "some_file_path", 6);
Run Code Online (Sandbox Code Playgroud)

你可以看看反编译@sharplab

这是在应用程序中编译的东西吗

是的

或者一些相关的反射,或者其他什么?

不,没有反映。