[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

clone of an inherited generic list

Posted on 2010-08-27
5
Medium Priority
?
734 Views
Last Modified: 2013-11-08
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<NavigationElement> : 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<NavigationElement>, 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?
0
Comment
Question by:LorenzoB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:LorenzoB
ID: 33543064
Actually resolved in this way (maybe useful for some other).
 public NavigationElementsList<NavigationElement> FindAll(Predicate<NavigationElement> match)
        {
            NavigationElementsList<NavigationElement> navCopyOfList = new NavigationElementsList<NavigationElement>();
            navCopyOfList.AddRange(base.FindAll(match));
            return navCopyOfList;
        }
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33543074
0
 

Author Comment

by:LorenzoB
ID: 33543110
The problem is in the FindAll method and the type that it returns.
Thank you anyway
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 2000 total points
ID: 33579507
In the first place why do you want to inherit from List<T>. I suggest you to write an extension method instead to add your clone method

say like

public T Clone<T>(this List<T> _list)
        {
            return _list.MemberwiseClone(); // Or something like this
        }
0
 

Author Comment

by:LorenzoB
ID: 33581765
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
 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; 
            }
        }

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question