gpt4 book ai didi

c# - CallerMemberName/CallerFilePathAttribute/CallerLineNumber 对性能的影响

转载 作者:行者123 更新时间:2023-12-03 07:51:31 25 4
gpt4 key购买 nike

使用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);
}

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

最佳答案

此属性由编译器处理,即 TraceMessage("Something gone."); 将被编译器替换为如下内容:

TraceMessage("Something happened.", "DoProcessing", "some_file_path", 6);

可以查看反编译@sharplab

Is this something that gets compiled in the application

是的

or something related reflection, or something else?

不,没有反射。

关于c# - CallerMemberName/CallerFilePathAttribute/CallerLineNumber 对性能的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76968517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com