3
2

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 1 year has passed since last update.

C#で非推奨のクラス、メソッドに警告を表示する

Posted at

1. はじめに

  • Javaで使用していた@DeprecatedアノテーションのようにC#でも同じように非推奨クラス、メソッドに警告を表示したい

2. ObsoleteAttribute クラス

  • クラス、メソッドに[Obsolete]属性を指定することで対応できる (Microsoftサイト参照)

2.1.警告にする場合

  • 属性に[Obsolete("任意のメッセージ")]をつける
    image.png

2.2. コンパイルエラーにする場合

  • 属性に[Obsolete("任意のメッセージ",false)]をつける
    image.png

2.3. コンストラクタ

image.png

3. 動作確認

3.1. サンプルコード

        private void Form_Load(object sender, EventArgs e)
        {
            ExecuteWarning();
            ExcecuteError();
        }

        [Obsolete("Warning")]
        public static void ExecuteWarning()
        {
            Debug.Print("Warning!");
        }

        [Obsolete("Error",true)]
        public static void ExcecuteError()
        {
            Debug.Print("Error!");
        }

3.2. VisualStduio

  • コードエディタに警告、エラーの波線が表示される
    image.png
  • 想定通り警告。エラーが表示される
    image.png

4. 参考文献

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?