尝试序列化Dictionary时,SerializationException“未标记为可序列化”

时间:2019-01-24 09:05:28

标签: c# forms serialization

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!"); }
        }
    }


Tracks类:

[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);
        }


    }


预先感谢您的回答!

1 个答案:

答案 0 :(得分:0)

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

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