デバッグをするとき、ちょっと必要になったのでメモ。
ファイル名 , クラス名 , メソッド名 の 取得 サンプル
namespace MvcApplication.Filters
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
public class AuthorizeFilterAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
// アセンブリファイル名 の 取得
var fileName = Path.GetFileName(this.GetType().Assembly.Location);
// クラス名 の 取得
var className = this.GetType().FullName;
// メソッド名 の 取得
var methodName = MethodBase.GetCurrentMethod().Name;
// デバッグ出力
System.Diagnostics.Debug.WriteLine("{0,30} {1,30} : {2}", fileName, className, methodName);
}
}
}
今回、以下のサイトを参考にしました。
- Landscape - エンジニアのメモ - 現在実行中のメソッド名を取得する MethodBase.GetCurrentMethod()
- プログラム番長のヲボエガキ - C#でクラス名を文字列で取得
- MSDN - MethodBase クラス
最後に… このブログに興味を持っていただけた方は、 ぜひ 「Facebookページ に いいね!」または 「Twitter の フォロー」 お願いします!!