Cast Object to Generic List

Posted by CrazyJoe on Stack Overflow See other posts from Stack Overflow or by CrazyJoe
Published on 2010-05-14T19:38:22Z Indexed on 2010/05/14 19:44 UTC
Read the original article Hit count: 274

Filed under:
|
|

I have 3 generict type list.

List<Contact> = new List<Contact>();
List<Address> = new List<Address>();
List<Document> = new List<Document>();

And save it on a variable with type object. Now i nedd do Cast Back to List to perfom a foreach, some like this:

List<Contact> = (List<Contact>)obj;

But obj content change every time, and i have some like this:

List<???> = (List<???>)obj;

I have another variable holding current obj Type:

Type t = typeof(obj);

Can i do some thing like that??:

List<t> = (List<t>)obj;

Obs: I no the current type in the list but i need to cast , and i dont now another form instead:

List<Contact> = new List<Contact>();

Help Plz!!!

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics