Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

実行中のメソッドを宣言するクラスの名前を取得する

実行中のメソッドを宣言するクラスの名前を取得する方法を紹介します。ログ出力に便利です。

ソースコード

using System;
using System.Reflection;
namespace ConsoleApplication {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(MethodBase.GetCurrentMethod().DeclaringType.Name);        // クラス名のみ
            Console.WriteLine(MethodBase.GetCurrentMethod().DeclaringType.FullName);    // 名前空間を含む
            Console.ReadLine();
        }
    }
}

出力

Program
ConsoleApplication.Program