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

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

问题描述

在一个非静态方法,我可以用 this.GetType(),它会返回键入。我怎样才能得到相同的键入的静态方法?当然,我不能只写的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!

推荐答案

如果你正在寻找一个1衬垫,等效于 this.GetType()静态方法,请尝试以下。

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)昂贵得多

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

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