我正在使用BlockingCollection並嘗試序列化它時遇到問題。該錯誤發生在新的XmlSerializer行上。錯誤是:BlockingCollection默認訪問器
您必須實現上System.Collections.Concurrent.BlockingCollection`1默認訪問[BlockingCollTest.MyItem,BlockingCollTest,版本= 1.0.0.0,文化=中立,公鑰=空],因爲它從ICollection繼承。
測試程序是:
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace BlockingCollTest
{
class Program
{
static void Main(string[] args)
{
BlockingCollection<MyItem> c = new BlockingCollection<MyItem>();
c.Add(new MyItem("001", "Smith"));
c.Add(new MyItem("002", "Johnson"));
XmlSerializer serializer = new XmlSerializer(typeof(BlockingCollection<MyItem>));
}
}
[Serializable]
public class MyItem
{
public string ID { get; set; }
public string Name { get; set; }
public MyItem() { }
public MyItem(string id, string name) { ID = id; Name = name; }
}
}
嘗試多種解決方案之後,我茫然地瞭解如何解決這個錯誤。
問題:解決BlockingCollection問題的序列化需要什麼?
序列化打算用作線程安全集合的類是非常值得懷疑的。解決方法是使用其ToArray()方法並序列化數組。 –