Sign in to follow this  
xsirxx

[.net] Using a generic class in a list?

Recommended Posts

Ok hopefully sum1 can help me out here, I cant seem to find a way to do this in C#, if possible... I have a some generic class that I need generic, IE: public class Foo<_T>; Now I need to put a bunch of those in a list without knowing what _T is... for instance: List<Foo> newlist = new List<Foo>(); Now you cant do that because it needs a generic... SO: List<Foo<...>> newlist = new List<Foo<...>>(); What do I put in the "..." considering I dont know what might be added there? I have a list of a bunch of different types but thats going to get crazy... Ohh also I need to be able to use functions in each Foo without knowing what the generic is... Any ideas would help me out alot! Thanks much, Brad EDIT: btw I did find that if u create a base class with no generic it will work correctly, but you cant explicitly cast the child class that has a generic. So all functions must be included as virtual or abstract in the base class...

Share this post


Link to post
Share on other sites
Part of what you're asking you can't really do easily because generics are strongly typed. The compiler can't verify a statement like newlist[3].DoSomething(a); because it won't be able to tell whether the type of object A matches the generic type of the fourth item in the list.

One option would be to Make Foo implement a non-generic interface IBar, and have your list be of List<IBar>. Then when you need to make type specific calls use reflection to inspect the generic arguments of the the members of the list.

Share this post


Link to post
Share on other sites
Quote:
Original post by kanato
Then when you need to make type specific calls use reflection to inspect the generic arguments of the the members of the list.


Heh i was just updating my post as you posted, that was QUICK! anywho, thanks much for the reply, any good places to lookup reflection like this under C#? If not no worries, ill dive deeper into it...

Thanks again

Share this post


Link to post
Share on other sites
Not sure if this is what you're getting at... but this might be helpful... no?


public class Foo<foo_object>
{
public Foo()
{
}
}

public class MyContainer<object_type>
{
List<Foo<object_type>> newlist = new List<Foo<object_type>>();

public MyContainer()
{
}
}


I think it might be helpful to know exactly what you're trying to do.

Share this post


Link to post
Share on other sites
Well, I don't know of any good resources aside from the msdn documentation. Here's basically the things I was thinking of:


static void Main(string[] args)
{
List<object> a = new List<object>();

a.Add(new List<Point>());
a.Add(new List<Size>());

DoStuff(a[0]);
DoStuff2(a[1]);

Console.ReadKey(false);
}

private static void DoStuff(object p)
{
if (p is List<Point>)
{
List<Point> myList = (List<Point>)p;

myList.Add(new Point(3, 3));
}
else if (p is List<Size>)
{
List<Size> myList = (List<Size>)p;

myList.Add(new Size(4, 2));
}
}
private static void DoStuff2(object p)
{
Type[] types = p.GetType().GetGenericArguments();

foreach (Type t in types)
Console.WriteLine(t.Name);
}




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this