.NET:在其静态方法中确定“this"类的类型 [英] .NET: Determine the type of “this” class in its static method

查看:16
本文介绍了.NET:在其静态方法中确定“this"类的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a non-static method I could use this.GetType() and it would return the Type. How can I get the same Type in a static method? Of course, I can't just write typeof(ThisTypeName) because ThisTypeName is known only in runtime. Thanks!

推荐答案

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

If you're looking for a 1 liner that is equivalent to this.GetType() for static methods, try the following.

Type t = MethodBase.GetCurrentMethod().DeclaringType

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

Although this is likely much more expensive than just using typeof(TheTypeName).

这篇关于.NET:在其静态方法中确定“this"类的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆