如果我有一个名为MyProgram的类,是否有一种方法检索“MyProgram”作为字符串?


当前回答

获取Asp.net的当前类名

string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();

其他回答

虽然micahtan的答案很好,但它在静态方法中不起作用。如果你想检索当前类型的名称,这个方法应该在任何地方都适用:

string className = MethodBase.GetCurrentMethod().DeclaringType.Name;

这个可以省略。所有你需要得到当前类名的是:

GetType().Name

这可以用于泛型类

typeof (T)。的名字

我还想把这个吐出来。我认为@micahtan的发帖方式更可取。

typeof(MyProgram).Name

获取Asp.net的当前类名

string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();