Generic method to create deep copy of all elements in a collection

Posted by bwarner on Stack Overflow See other posts from Stack Overflow or by bwarner
Published on 2010-04-30T13:21:40Z Indexed on 2010/04/30 13:27 UTC
Read the original article Hit count: 674

Filed under:
|

I have various ObservableCollections of different object types. I'd like to write a single method that will take a collection of any of these object types and return a new collection where each element is a deep copy of elements in the given collection. Here is an example for a specifc class

   private static ObservableCollection<PropertyValueRow> DeepCopy(ObservableCollection<PropertyValueRow> list)
   {
        ObservableCollection<PropertyValueRow> newList = new ObservableCollection<PropertyValueRow>();
        foreach (PropertyValueRow rec in list)
        {
            newList.Add((PropertyValueRow)rec.Clone());
        }
        return newList;
   }

How can I make this method generic for any class which implements ICloneable?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics