LorenzoB
asked on
clone of an inherited generic list
I have a class "NavigationElement", where I implemented a clone method.
Then I have a generic List that inherits from a list of this objects (NavigationElement)
public class NavigationElementsList<Nav igationEle ment> : List<NavigationElement>, ICloneable
...
in this list I also implemented the
public Object Clone()
{
return this.MemberwiseClone();
}
now I need to call the FindAll method in an istance of this NavigationElementsList<Nav igationEle ment>, but this is not possible because the returned type is not the same as the generic List<T>.
I need to have a deep copy of the list because I'm going to change the values of the properties and this will cause a change in the referenced items in the original list<T> that I want to avoid.
This also won't work:
public NavigationElementsList<T> FindAll(Predicate<T> match)
{
return base.FindAll(match);
}
Any suggestions?
Then I have a generic List that inherits from a list of this objects (NavigationElement)
public class NavigationElementsList<Nav
...
in this list I also implemented the
public Object Clone()
{
return this.MemberwiseClone();
}
now I need to call the FindAll method in an istance of this NavigationElementsList<Nav
I need to have a deep copy of the list because I'm going to change the values of the properties and this will cause a change in the referenced items in the original list<T> that I want to avoid.
This also won't work:
public NavigationElementsList<T> FindAll(Predicate<T> match)
{
return base.FindAll(match);
}
Any suggestions?
ASKER
The problem is in the FindAll method and the type that it returns.
Thank you anyway
Thank you anyway
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To answer to your question, I needed to Inherit from a List<T> because I want to have the FindAll and the Clone (DeepCopy not Shallow).
Your suggestion was indeed helpful.
MemberwiseClone() cannot be called by an extension method of a generics list because it is a private member.
So I did a sort of implementation of the MemberwiseClone.
In the following extract of code are the Extension methods. Maybe could be better written...
The class NavigationElement inherits from ICloneable and implements the Clone() method.
In this way I avoid to create a new Class that inherits from List<NavigationElement>, because the implementation of the Extension Methods.
Thank You
Your suggestion was indeed helpful.
MemberwiseClone() cannot be called by an extension method of a generics list because it is a private member.
So I did a sort of implementation of the MemberwiseClone.
In the following extract of code are the Extension methods. Maybe could be better written...
The class NavigationElement inherits from ICloneable and implements the Clone() method.
In this way I avoid to create a new Class that inherits from List<NavigationElement>, because the implementation of the Extension Methods.
Thank You
public static class ListExtensions
{
public static object Clone<T>(this List<NavigationElement> list)
{
return list.MemberwiseClone<T>();
}
public static List<NavigationElement> MemberwiseClone<T>(this List<NavigationElement> list)
{
List<NavigationElement> newList = new List<NavigationElement>();
foreach (NavigationElement navElem in list)
{
newList.Add((NavigationElement)navElem.Clone());
}
return newList;
}
}
ASKER
public NavigationElementsList<Nav
{
NavigationElementsList<Nav
navCopyOfList.AddRange(bas
return navCopyOfList;
}