Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Extensions
- {
- public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
- {
- return listToClone.Select(item => (T)item.Clone()).ToList();
- }
- }
- List<YourType> newList = new List<YourType>(oldList);
- List<ICloneable> oldList = new List<ICloneable>();
- List<ICloneable> newList = new List<ICloneable>(oldList.Count);
- oldList.ForEach((item) =>
- {
- newList.Add((ICloneable)item.Clone());
- });
- List<YourType> oldList = new List<YourType>();
- List<YourType> newList = new List<YourType>(oldList.Count);
- oldList.ForEach((item)=>
- {
- newList.Add(new YourType(item));
- });
- public static object DeepClone(object obj)
- {
- object objResult = null;
- using (MemoryStream ms = new MemoryStream())
- {
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(ms, obj);
- ms.Position = 0;
- objResult = bf.Deserialize(ms);
- }
- return objResult;
- }
- List<int> oldList = new List<int>( );
- // Populate oldList...
- List<int> newList = oldList.GetRange(0, oldList.Count);
- List<int> newList = new List<int>(oldList);
- List<T> Clone<T>(IEnumerable<T> oldList)
- {
- return newList = new List<T>(oldList);
- }
- List<string> myNewList = Close(myOldList);
- public static Object CloneType(Object objtype)
- {
- Object lstfinal = new Object();
- using (MemoryStream memStream = new MemoryStream())
- {
- BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
- binaryFormatter.Serialize(memStream, objtype); memStream.Seek(0, SeekOrigin.Begin);
- lstfinal = binaryFormatter.Deserialize(memStream);
- }
- return lstfinal;
- }
- public class CloneableList<T> : List<T>, ICloneable where T : ICloneable
- {
- public object Clone()
- {
- var clone = new List<T>();
- ForEach(item => clone.Add((T)item.Clone()));
- return clone;
- }
- }
- public class CloneableList<T> : List<T>, ICloneable where T : ICloneable
- {
- public object Clone()
- {
- var clone = new List<T>();
- ForEach(item => clone.Add((T)item.Clone()));
- return clone;
- }
- }
Add Comment
Please, Sign In to add comment