你想要做什么是深拷贝名单,所以你可以使用这个扩展:
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
public static class ObjectCopier
{
/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
现在,我可能会问你为什么这样做,你为什么不通过名单另一个线程和多数民众赞成在所有(鉴于你修改它回来)。如果它是一个UI控件(可能是我看到的),那么可以使用背景ItemsSource(来自其他线程),然后使用Dispatcher在UI上将其用作源。如果这就是你想让我知道提供更多细节。
只是不。只在线程中创建数据。填充它,比如说一个List <>。只在UI线程上填充ListView。 BackgroundWorker使其变得简单。 – 2012-04-22 17:39:23
是的,@Hans说了什么。必须使用适当的数据源。 – IAbstract 2012-04-22 17:54:32
这就是我所做的。我只想复制整个ListView,因为结构已经存在。我刚刚创建了一个具有相同列的新结构,然后将其逐行地复制到实际的结构中。 – CJxD 2012-05-02 07:53:03