Passing several typesafe Lists to a method?

G

Guest

Hi all,

What is the syntax for a method that accepts a list of generic lists?

For example, I have a List that contains the following lists:

List<Car>
List<Boat>
List<Plane>

I want to pass this "list of lists" to a method so the list contents can be
persisted. For example, I am trying to do something like this (but I am lost
for the right syntax):

SaveObjectsToDisk(List<T> lists)
{
foreach(List list in lists)
{
foreach(Type T in list) db.Save(T);
}
}

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

If the method is going to be generic (in the sense that you will use it
for lists of lists of multiple types, and not always for Car, Boat, and
Plane lists) then the only way I can see this happening is if you use
reflection.

You can't use List<List<T>> since that would fix all the lists.

Rather, you should take an IList (the non-Generic version) and then
cycle through those, making sure that each element is an IList<T>. Use
reflection to find out what <T> is and then call the appropriate save
method.

Your example won't compile, btw, since you are trying to declare an
instance variable T when you have a type parameter of the same name.
 
B

bob

Hi Adam,
Assuming examples below were not just accidentally related how about
using a list of the base class 'Vehicle'.
Leave it to the DAL to figure out the saving technique.
regards
Bob

On Mon, 27 Aug 2007 13:36:02 -0700, Adam M <Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top