C# 如何在运行时获取当前类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12035426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:09:21  来源:igfitidea点击:

How to get the current class name at runtime?

c#classreflectionabstract

提问by Craig

I'm trying to get a current class name into a string.

我正在尝试将当前类名放入字符串中。

For example:

例如:

public class Marker : Mark
{
    string currentclass = ???;
}

public abstract class MiniMarker : Mark
{
}

I'd like to get the string from Markerclass so I do not have to put it inside each abstract class I make from it.

我想从Marker类中获取字符串,所以我不必将它放在我从中创建的每个抽象类中。

I want the string to be MiniMarker, or what ever the abstract class is named.

我希望字符串是MiniMarker,或者抽象类的名称。

I tried MethodBase.GetCurrentMethod().DeclaringType, but it did not work.

我试过了MethodBase.GetCurrentMethod().DeclaringType,但没有用。

回答by Oded

This should do:

这应该做:

this.GetType().ToString()

回答by taffarel

   this.GetType().Name

should return a Class name

应该返回一个类名