c# print the class name from within a static function

前端 未结 7 759
囚心锁ツ
囚心锁ツ 2021-02-06 23:19

Is it possible to print the class name from within a static function?

e.g ...

public class foo
{

    static void printName()
    {
        // Print the          


        
7条回答
  •  隐瞒了意图╮
    2021-02-06 23:58

    Since C# 6.0 there exists an even simpler and faster way to get a type name as a string without typing a string literal in your code, using the nameof keyword:

    public class Foo
    {
        static void PrintName()
        {
            string className = nameof(Foo);
            ...
        }
    }
    

提交回复
热议问题