CyberRascal
September 16th, 2008, 01:30 PM
Hey.
I have a List<DataModel> called object. At run-time I do not know what type this DataModel is, only that is derives from Model. Now, I must do a for-each loop on the elements in this list. I cannot simply cast it by the following example:
object = new List<DataModel>();
List<Model> models = (List<Model>)object;
Is it possible to somehow perform a for-each loop or cast the list<DataModel> to a List<Model>?
It would be great if there was a method similar to Array.CreateInstance(Type type), which creates an array of a specified Type, but that converted each item in a list to the specified Type.
There is an extension method called Cast, which is used to cast the current list to another type T, which would be fine, as I know I want to convert it to Model, but I can't use that method since I can't cast List<DataModel> to a List since the List needs a type definition. Any tips?
I have a List<DataModel> called object. At run-time I do not know what type this DataModel is, only that is derives from Model. Now, I must do a for-each loop on the elements in this list. I cannot simply cast it by the following example:
object = new List<DataModel>();
List<Model> models = (List<Model>)object;
Is it possible to somehow perform a for-each loop or cast the list<DataModel> to a List<Model>?
It would be great if there was a method similar to Array.CreateInstance(Type type), which creates an array of a specified Type, but that converted each item in a list to the specified Type.
There is an extension method called Cast, which is used to cast the current list to another type T, which would be fine, as I know I want to convert it to Model, but I can't use that method since I can't cast List<DataModel> to a List since the List needs a type definition. Any tips?