メソッドの引数に属性を付けることによって、デバッグ用診断情報をメソッドへ渡すことができます。
属性を付ける引数は、デフォルト値が指定されている必要があります。
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イベントを発火させるために、プロパティ名を指定する手間を省略することが可能になります。