CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2008
    Posts
    29

    Type-casting by type.

    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:

    Code:
    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?

  2. #2
    Join Date
    Jul 2008
    Posts
    29

    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.

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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 especially when working with patterns and models. Basically said I would say always prefer interfaces over inheritance.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Jul 2008
    Posts
    29

    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

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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".
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured