コードインスペクション:DependencyProperty を登録する際に 'nameof' 式を使用する
このインスペクションでは、 nameof 式を使用するとコードの保守性が向上し、エラーが発生しにくくなるため、 DependencyProperty を登録するときに nameof 式を使用することを推奨しています。
以下の例では、 "PageVisibility" の代わりに nameof(PageVisibility) を使用すると、コードがより堅牢になり、プロパティ名のコンパイル時のチェックが提供されます。 後で PageVisibility プロパティの名前を変更すると、 nameof 式により、新しい名前を反映するように引数を更新する必要があります。 この方法では、一貫性が確保され、実行時エラーのリスクが軽減されます。
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register("PageVisibility",
typeof(Visibility),
typeof(PagePreview),
null);
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register(nameof(PageVisibility),
typeof(Visibility),
typeof(PagePreview),
null);
2026 年 6 月 12 日