gpt4 book ai didi

c# - .NET:在其静态方法中确定 “this” 类的类型

转载 作者:IT王子 更新时间:2023-10-29 03:37:39 26 4
gpt4 key购买 nike

在非静态方法中,我可以使用 this.GetType(),它会返回 Type。如何在静态方法中获得相同的 Type?当然,我不能只写 typeof(ThisTypeName) 因为 ThisTypeName 只有在运行时才知道。谢谢!

最佳答案

如果您正在为静态方法寻找等效于 this.GetType() 的 1 衬里,请尝试以下操作。

Type t = MethodBase.GetCurrentMethod().DeclaringType

尽管这可能比仅使用 typeof(TheTypeName) 昂贵得多。

关于c# - .NET:在其静态方法中确定 “this” 类的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2081612/

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