gpt4 book ai didi

c# 从静态函数中打印类名

转载 作者:IT王子 更新时间:2023-10-29 04:15:08 24 4
gpt4 key购买 nike

是否可以从静态函数中打印类名?

例如...

public class foo
{

static void printName()
{
// Print the class name e.g. foo
}

}

最佳答案

您可以通过三个选项来获取在静态函数中工作的 YourClass 的类型(以及名称):

  1. typeof(YourClass) - 快(0.043 微秒)

  2. MethodBase.GetCurrentMethod().DeclaringType - 慢(2.3 微秒)

  3. new StackFrame().GetMethod().DeclaringType - 最慢(17.2 微秒)


如果不希望使用 typeof(YourClass),那么 MethodBase.GetCurrentMethod().DeclaringType 绝对是最佳选择。

关于c# 从静态函数中打印类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/552629/

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