Hi,
How can do runtime casting?
MyCollection derives from ArrayList
I will store lost of different objects that all derive from the same parent
class. I then want to be able to pass in the object type and collection type
I have a number of classes that derive from ArrayList and have if pick out
on ly those i asked for.
Problem is it wont let me cast using a type as a method argument.
//public method in instance of MyCollection
public MyCollection GetTypesFromCollection(Type ObjectType, Type
CollectionType)
{
MyCollection s;
(CollectionType)s = new MyCollection(); //create collection and cast to
runtime collection type
foreach(object o in this) //loop through all entries in this arraylist
{
if(o.GetType() == ObjectType.GetType()) //if this object type is the
same as the type I'm asking for
{
s.Add( (ObjectType) o); //add it to the tryped arraylist
}
}
return s; //give it all back
}
Help!!