c# print the class name from within a static function

前端 未结 7 775
囚心锁ツ
囚心锁ツ 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:42

    A (cleaner, IMO) alternative (still slow as hell and I would cringe if I saw this in a production code base):

    Console.WriteLine(MethodBase.GetCurrentMethod().DeclaringType);
    

    By the way, if you're doing this for logging, some logging frameworks (such as log4net) have the ability built in. And yes, they warn you in the docs that it's a potential performance nightmare.

提交回复
热议问题