9
11

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.

INotifyPropertyChanged をつけたクラスのシリアライズが出来ない場合

Last updated at Posted at 2015-10-10

例えばこんなクラスがあります。

CustomClass
        [Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            public event PropertyChangedEventHandler PropertyChanged; ///エラーが起きる箇所
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

これをコンパイルすると、
public event PropertyChangedEventHandler PropertyChanged;
がシリアライズ出来ないとしてエラーが発生します。

このために、event部分をNonSerializedに指定する必要があるのですが、

CustomClass
        [Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            [NonSerialized] ///変更点ここ
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

とやるとまたしてもエラーが起きます。
NonSerializedがフィールドタイプではない、と怒られるので、フィールドタイプとしてきちんと指定する必要があります。

CustomClass
        [Serializable]
        public class CustomClass : INotifyPropertyChanged
        {
            private bool _checkbox;
            public int Value { get; set; }
            public bool Checkbox
            {
                get
                {
                    return _checkbox;
                }
                set
                {
                    _checkbox = value;
                    NotifyPropertyChanged("Checkbox");
                }
            }

            public int SecondValue { get; set; }
            public string Text { get; set; }
            [field: NonSerialized] ///変更点ここ
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

これで INotifyPropertyChanged のあるクラスのシリアライズが出来ます。

9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?