Okada Hiroshi の blog

typo が多いです

INotifyPropertyChanged の実装が面倒くさい (Google Apps Script を使ってスニペットを生成したけどそれでも面倒くさい)

久しぶりに Windows アプリケーションを C# で書いているのですが、INotifyPropertyChanged の実装が非常に面倒くさいです。正直言って非人間的だと思います。

仕方がないので、Google Apps Script でコードの断片を自動生成することにしました。

具体的な方法としては

(1) Google スプレッドシートを開いて、さらにスクリプトエディタを開いて以下のスクリプトを作成しました。

gistd911b45aa32a99e5c0be

(2) スプレッドシートを閉じて開いて、以下のように f:id:OkadaHiroshi:20160314175648p:plain のように入力しました。

(3) メニューからスニペット作成→INotifyPropertyChangedスニペットを実行し、以下のような文書が作成されるのでこれをコピーしてプログラムにペーストしました。

        private int testOne = 0;
        private string testTwo = "";

       /// <summary>テスト1</summary>
        public int TestOne        {
            get { return testOne; }
            set
            {
                if (value != testOne) {
                    testOne = value;
               }
               NotifyPropertyChanged("TestOne");
            }
        }


        public string TestTwo        {
            get { return testTwo; }
            set
            {
                if (value != testTwo) {
                    testTwo = value;
               }
               NotifyPropertyChanged("TestTwo");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

面倒臭さが 100 から 50 くらいには減りましたが、いちいちコピペするのは全くイケていません。外部ライブラリ等を導入するのも躊躇するしみんなどうしているのだろう。