Re: Type-casting by type.
Hello. I found the solution to my problem on my own. Seems there were many solutions, one of them casting to interface IList which provides access to an enumerator without having to declare the type of objects in the list.
Thanks.
Re: Type-casting by type.
Quote:
Originally Posted by CyberRascal
Hello. I found the solution to my problem on my own. Seems there were many solutions, one of them casting to interface IList which provides access to an enumerator without having to declare the type of objects in the list.
Thanks.
Yes there are maybe many solutions, but I want to acknowledge this as IMHO this is one of the best solutions for your problems :wave: especially when working with patterns and models. Basically said I would say always prefer interfaces over inheritance.
Re: Type-casting by type.
Hey, thanks for answering :)
I just had another follow-up question. It's not really relevant in my project, but IList does not provide access to methods like Find, Sort or other useful stuff like that. Is there any way to access these methods without at compile-time knowing the type?
Thank you :)
Re: Type-casting by type.
The best way would be to instantiate a List<Model> to start off with rather than a List<DataModel> if that's possible.
Re: Type-casting by type.
Following doesn't work?
Code:
List<DataModel>() dataModels = new List<DataModel>();
foreach (Model e in dataModels)
{
// do something
}
Generally, if you are facing working with type unknown at runtime, I would recommend you to focus on interfaces and go the way of "desing by contract".