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!
Adam M - 28 Aug 2007 15:24 GMT
Just a ping to see if anyone has any ideas on this one...
Thanks!
> Hi all,
>
[quoted text clipped - 19 lines]
>
> Thanks!
Nicholas Paldino [.NET/C# MVP] - 28 Aug 2007 17:13 GMT
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.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi all,
>
[quoted text clipped - 21 lines]
>
> Thanks!
bob - 30 Aug 2007 05:54 GMT
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
>Hi all,
>
[quoted text clipped - 19 lines]
>
>Thanks!