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
Solved

Generic Extension Method Problem

Posted on 2010-09-14
5
389 Views
Last Modified: 2012-05-10
I'm trying to extend a wrapper of generic objects ('Retreival' in the example below) to output as a generic list, but I can't get it to work. The generic identifiers aren't recognized?
public static class Exts
    {
        public static List<T> ToList(this  Retrieval<T> lstToUse)
        {
            List<T> lstRet = new List<T>();
            foreach(T item   in  lstToUse.Results<T>)
            {
                lstRet.Add(item);
            }
            return lstRet;
        }
    }
0
Comment
Question by:Silas2
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33671248
are you getting any error ?
0
 
LVL 2

Expert Comment

by:SandyAgo
ID: 33671267
Hi Silas2,

I am a little unclear on what you are trying to achieve here. This might work for you. If not, can you please give some more info on the design and requirement behind this functionality?
public static class Exts
    {
        public static List<T> ToList<T>(List<T> input)
        {
            List<T> list = new List<T>();
            foreach (T item in input)
            {
                list.Add(item);
            }
            return list;
        }

    }

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 125 total points
ID: 33671310
assuming your retrival class is something like
public class Retrieval<T>
    {
        public IEnumerable<T> Results
        {
            get;
            set;
        }
    }

seethe modified code below

public static class Ext
    {
        public static List<T> ToList<T>(this  Retrieval<T> lstToUse)
        {
            List<T> lstRet = new List<T>();
            foreach (T item in lstToUse.Results)
            {
                lstRet.Add(item);
            }
            return lstRet;
        }
    }

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33671405

public static List<T> Clone<T>(this List<T> _list)
            where T : ICloneable
        {
            List<T> newList = new List<T>();

            foreach (T item in _list)
                newList.Add((T) item.Clone());

            return newList;
        }

public class Retreival : ICloneable
{
public object Clone()
        {
            return MemberwiseClone();
        }
}

Open in new window

0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 125 total points
ID: 33671411
Cloning is safer as your code just copies the reference unless you really intended that.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 46
Selenium docs api java index 3 48
How to read text with RegEx... 2 25
programming a polycom voip phone 3 18
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

840 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