0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

呼び出し元の情報を引数へ渡す。(Caller Info 属性)

Posted at

メソッドの引数に属性を付けることによって、デバッグ用診断情報をメソッドへ渡すことができます。
属性を付ける引数は、デフォルト値が指定されている必要があります。
C言語の__FILE__や__LINE__マクロのような使い方ができます。

DebugLog.cs
public static class DebugLog
{
    public static void WriteLine(string message,
        [CallerFilePath] string file = "",
        [CallerLineNumber] int line = 0,
        [CallerMemberName] string member = "")
    {
    }
}

デバック以外の実用的な使い方として、INotifyPropertyChangedの実装が挙げられます。

NotificationObject.cs
public class NotificationObject : INotifyPropertyChanged
{
    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName="")
    {
    }
}

PropertyChangedイベントを発火させるために、プロパティ名を指定する手間を省略することが可能になります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?