C# 在序列化期间从另一个线程更改对象
考虑以下对象:C# 在序列化期间从另一个线程更改对象,c#,multithreading,serialization,C#,Multithreading,Serialization,考虑以下对象: class MyObject { public string StringValue { get; set; } public List<int> Numbers { get; set; } } BinarySerializer和XmlSerializer根本不处理这个问题。如果您需要类似的东西,那么您可以在序列化程序和可序列化对象之间建立某种“契约”。我不认为有一种好方法可以使这种通用性适用于任何类型的对象。@I4V,我不知道这是不是一个玩笑……我
class MyObject
{
public string StringValue { get; set; }
public List<int> Numbers { get; set; }
}
BinarySerializer和XmlSerializer根本不处理这个问题。如果您需要类似的东西,那么您可以在序列化程序和可序列化对象之间建立某种“契约”。我不认为有一种好方法可以使这种通用性适用于任何类型的对象。@I4V,我不知道这是不是一个玩笑……我想是这样,但不确定。似乎他们将这一责任委托给了序列化程序的使用者。我没有问题遵循这种模式(我要做的更少)。
class Program
{
private static List<int> _list;
static void Main(string[] args)
{
_list = new List<int>{1,2,3};
var thread = new Thread(Edit) {IsBackground = true};
lock (_list)
{
_list.Add(4);
thread.Start();
Thread.Sleep(20);
_list.Add(100);
}
while (thread.IsAlive) {}
_list.ForEach(Console.WriteLine);
Console.ReadLine();
}
static void Edit()
{
for (int i = 5; i < 35; i++)
{
_list.Add(i);
Thread.Sleep(1);
}
}
}