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>();
The Java compiler won't let you cast a generic type across its type parameters because the target type, in general, is neither a subtype nor a supertype.
The Generic List<T> Class in C# is a collection class that is present in System. Collections. Generic namespace. This Generic List<T> Collection Class represents a strongly typed list of objects which can be accessed by using the index.
Lots of trial and error gave me this on SL 5 but it should also work on a regular C#. You also need to add LINQ to your using list for the last half to work.
List<object> myAnythingList = (value as IEnumerable<object>).Cast<object>().ToList()
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With