How to deep-copy the ListCollectionView?
-
I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Deep copy, as in clone the objects too?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Michael Sync wrote:
yes but i dont want to have an reference of the original object.
Which object, the list or the contents? Is Person clonable?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)leppie wrote:
Which object, the list or the contents?
Yes. the list of person.
leppie wrote:
Is Person clonable?
No. (Actually, I'm using ListCollectionView instead of List in my project. AFAIK, ListCollectionView is not a clonable ) Thanks.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
leppie wrote:
Which object, the list or the contents?
Yes. the list of person.
leppie wrote:
Is Person clonable?
No. (Actually, I'm using ListCollectionView instead of List in my project. AFAIK, ListCollectionView is not a clonable ) Thanks.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
Michael Sync wrote:
leppie wrote: Is Person clonable? No.
Then it will be a shallow clone, with
new List<Person>(yourlistcollectionview)
, you maybe have to cast/convert the ListCollectionView' IEnumerable to a IEnumerable<Person> first.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
(I'm not sure what a "ListCollectionView" is, but I can give some general information on creating a deep-copy of an object graph) well...there are several approaches you could take. All have their pros and cons...here's one technique. Mark all of your objects in your object graph with the Serializeable attribute. There are 2 noticeable cons to this technique: One- you have to mark each class individually, so there is a chance that one could be accidentally omitted. Two- You don't want to serialize objects that contain references to external resources (databaseConnections being a prime example)
Your procedure (in pseudo-code)
Create a BinaryFormatter
Create a MemoryStream to store the serialized buffer of your object graph
formatter.Serialize(buffer, source) //the only line of code I'll give
return the clone by Deserializing the buffer and applying a C-cast to the stream.
The Serializeable attribute will take care of the restThe objects in the graph DO NOT have to be ICloneable. The Serializeable attribute is all that is needed. You do, however need to mark anything that you do not want to be contained in the clone as Transient
"I need build Skynet. Plz send code"
-
yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
Iterate through each items in the
persons
collection, create new items with the same value and add tonewPersons
. This will be the most easiest way. Something likeforeach(Person person in persons)
{
newPersons.Add(new Person(person.Name,person.Email,...));
}You will have new instances with same value in the
newPersons
collection. Is this what you are looking for or I got you wrong ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
(I'm not sure what a "ListCollectionView" is, but I can give some general information on creating a deep-copy of an object graph) well...there are several approaches you could take. All have their pros and cons...here's one technique. Mark all of your objects in your object graph with the Serializeable attribute. There are 2 noticeable cons to this technique: One- you have to mark each class individually, so there is a chance that one could be accidentally omitted. Two- You don't want to serialize objects that contain references to external resources (databaseConnections being a prime example)
Your procedure (in pseudo-code)
Create a BinaryFormatter
Create a MemoryStream to store the serialized buffer of your object graph
formatter.Serialize(buffer, source) //the only line of code I'll give
return the clone by Deserializing the buffer and applying a C-cast to the stream.
The Serializeable attribute will take care of the restThe objects in the graph DO NOT have to be ICloneable. The Serializeable attribute is all that is needed. You do, however need to mark anything that you do not want to be contained in the clone as Transient
"I need build Skynet. Plz send code"
Alaric_ wrote:
(I'm not sure what a "ListCollectionView" is
ListCollectionView[^] Yes. Binary Serialization would be great.. I found an example that shows how to deep-copy the object using XmlFormater but that guy said that we should consider using Binary Serialization... But the problem is that there is no [Serializeable] attribute in ListCollectionView. and I can't modify the code since ListCollectionView is a part of .NET framework..
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Michael Sync wrote:
leppie wrote: Is Person clonable? No.
Then it will be a shallow clone, with
new List<Person>(yourlistcollectionview)
, you maybe have to cast/convert the ListCollectionView' IEnumerable to a IEnumerable<Person> first.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)Sorry for making you confused. Let me elaborate it again.. This is our real scenario... We are using WPF and Data-binding. We are using ListCollectionView to bind the control (e.g. Datagrid, TreeView or etc) ... The reason why we are using ListCollectionView is that it supports sorting, filtering and etc... So, our code will be like that. Note: We are using MVVM pattern in our project so we dont want to write the code in code-behind. We wrote the most of code in ViewModel and bind that ViewModel with View.. ===== Class ===== class Person() : INotification { private int _id; private string _Name; private List myPets = new List; public int ID{ /// } public string Name{ /// } public List MyPets{ // } //impelementation for INotification. } then.. I bind it with TreeView. =========== View - XAML =========== Souce="Pet" // child class ... /> .... Pet.Name /> ItemSouce={... personListViewCollection } //personListViewCollection is the object from code below ... /> =========== ViewModel =========== ListViewCollection personListViewCollection ; void Constructor(){ populateData(); } void populateData(){ List persons = new List(); Person p1 = new Person(); p1.ID =1; .. List pets = new List(); pets.Add(new Pet(){ ...... } ); pets.Add(new Pet(){ ...... } ); p1.MyPets.AddRange(pets); person.Add(p1); /// add p2 p3 p4 ///note: // Let's say personListViewCollection.GetHashCode() == 1000100 personListViewCollection = new ListViewCollection(persons); } So, those data will be displayed on TreeView.. then, I wanna re-populatethe data. void reload(){ List newPersons = new List(); Person newP1 = new Person(); newP1.ID =1; .. List newPets = new List(); newPets.Add(new Pet(){ ...... } ); newPets.Add(new Pet(){ ...... } ); newP1.MyPets.AddRange(newPets); newPersons.Add(newP1); ///HERE is the problem. I dont want the hashcode of personListViewCollection to change. personListViewCollection = new ListViewCollection(newPersons); //Problem.. } then, I want to refresh the treeview and display the new data... Any Idea?
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)