【问题标题】:SerializationException "is not marked as serializable" when trying to serialize a Dictionary尝试序列化字典时,SerializationException“未标记为可序列化”
【发布时间】:2019-01-24 17:10:05
【问题描述】:
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 播放器,我希望能够创建播放列表。我读到字典被标记为 [Serializable](并且实现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);
        }


    }


提前感谢您的任何回答!

【问题讨论】:

    标签: c# forms serialization


    【解决方案1】:

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

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

    【讨论】:

    • 哦,你是对的。谢谢,我会将你的答案标记为正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2015-02-15
    • 2017-02-21
    • 2015-11-10
    • 2016-01-18
    相关资源
    最近更新 更多