序列化尝试序列化字典时"is not marked as serializable"异常


Full error : System.Runtime.Serialization.SerializationException: 'Type 'TagLib.Id3v2.AttachmentFrame' in Assembly 'taglib-sharp, Version=2.1.0.0, Culture=neutral, PublicKeyToken=db62eba44689b5b0' is not marked as serializable.'



我正在尝试序列化表单应用程序的字典。我正在制作一个mp3播放器,我希望能够创建播放列表。我读到字典被标记为[可序列化](并实现ISerializable),因此序列化应该是可能的,所以我认为我可以使用它。但是,当我尝试序列化一个时,出现上述错误。我错过了什么吗?字典不应该可以在没有错误的情况下序列化吗?我的代码:

static Dictionary<String, Tracks> playlistTracks = new Dictionary<string, Tracks>();
        public Playlist()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string[] filenames, filepaths;
            openFileDialog1.Filter = "All Supported Audio | *.mp3; *.wma | MP3s | *.mp3 | WMAs | *.wma";
            if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filepaths = openFileDialog1.FileNames;
                filenames = openFileDialog1.SafeFileNames;
                for(int i = 0; i< filenames.Length; i++)
                {
                    if (!playlistTracks.ContainsKey(filenames[i]))
                    {
                        listBox1.Items.Add(filenames[i]);
                        Tracks track = new Tracks();
                        track.songName = filenames[i];
                        track.path = filepaths[i];
                        track.readMetaData(filepaths[i]);
                        playlistTracks.Add(filenames[i], track);
                    }
                }
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                if (listBox1.Items.Count > 0)
                {
                    Tracks.serializePlaylist(textBox1.Text, playlistTracks);
                }
            }
            else { MessageBox.Show("Choose a playlist name!"); }
        }
    }


轨道类:

[Serializable]
    class Tracks
    {
        public string  songName { get; set; }
        public string path { get; set; }
        public string album = "Unknown";
        public TimeSpan duration;
        public string artistName = "Unknown";
        public string musicGenre = "Unknown";
        public uint publishedYear = 0;
        private string language = "Unknown";
        private int playingFrequency = 0;
        public string title = "Unknown";
        public IPicture[] pictures;
        public bool picExists = false;
        public void readMetaData(string songPath)
        {
            #region AssignMetaDataToTrack
            var tfile = TagLib.File.Create(songPath);
            this.publishedYear = tfile.Tag.Year;
            if (tfile.Tag.FirstAlbumArtist !=null)
            {
                this.artistName = tfile.Tag.FirstAlbumArtist;
            }
            this.duration = tfile.Properties.Duration;
            if (tfile.Tag.FirstGenre !=null) {
                this.musicGenre = tfile.Tag.FirstGenre; }
            if (tfile.Tag.Album != null)
            {
                this.album = tfile.Tag.Album;
            }
            if (tfile.Tag.Title != null)
            {
                this.title = tfile.Tag.Title;
            }
            #endregion
            //check an uparxei artwork
            if (tfile.Tag.Pictures.Length > 0)
            {
                this.pictures = tfile.Tag.Pictures;
                picExists = true;
            }
        }

        public static void serializePlaylist(string playlistName , Dictionary<string,Tracks> playlist)
        {
            BinaryFormatter bf = new BinaryFormatter();
            Stream st = new FileStream(@"Playlists" + playlistName + ".txt", FileMode.OpenOrCreate);
            bf.Serialize(st, playlist);
        }

    }


提前感谢您的任何答案!

错误说明了一切:您在某处使用了未标记为可序列化的 TagLib.Id3v2.AttachmentFrame 类型。 也许在你的IPicture类型中,也许在其他地方。

如果可以控制库,请将类型标记为可序列化。 如果没有,则必须实现自己的自定义序列化。 或者避免使用此类型。 或者,最后,您可以决定不序列化包含 TagLib.Id3v2.AttachmentFrame 的类型的字段。

最新更新