Results 1 to 4 of 4

Thread: [2005] Get item type from Generic.List(of T) class

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    [2005] Get item type from Generic.List(of T) class

    Hi

    Given a class implementation like e.g.
    Code:
    Public Class MyControlList
       Inherits Generic.List(Of Control)
    
    End Class
    How would I go about finding out what <T> type the generic list is of?

    e.g.
    Code:
    Public Function ItemType(Byval CollectionType As Type) As Type
    ' ?? what magic code goes here
    End Function

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Get item type from Generic.List(of T) class

    Use something like:
    Code:
    If TypeOf ThingBeingChecked Is System.Windows.Forms.Label Then
        'Do something
    End If
    Read up on the TypeOf operator.

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Get item type from Generic.List(of T) class

    You can use the GetType function.

    for example
    Code:
            Dim l As New List(Of Control)
            l.Add(New TextBox)
            l.Add(New ComboBox)
    
            For Each lItem As Control In l
                MsgBox(lItem.GetType.ToString)
            Next
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Get item type from Generic.List(of T) class

    Firstly, I recommend inheriting System.Collections.ObjectModel.Collection(Of T) when creating a typed collection.

    Secondly, your MyControlList is NOT a generic class. It's just a class. If you wanted your class to be generic too then it would have to look like this:
    vb.net Code:
    1. Public Class ControlCollection(Of T As Control)
    2.     Inherits System.Collections.ObjectModel.Collection(Of T)
    3.  
    4. End Class
    Now your class is generic too because it has a generic type parameter. That generic type is limited to be Control or some type derived from Control, so you can do this in code:
    vb.net Code:
    1. Dim buttons As New ControlCollection(Of Button)
    2. Dim textBoxes As New ControlCollection(Of TextBox)
    etc. but this would fail to compile:
    vb.net Code:
    1. Dim strings As New ControlCollection(Of String)
    because the generic type you specified cannot be cast as type Control.

    Now, to your original question. You've specifically stated in your class declaration that your class inherits List(Of Control), so there's no generic type to determine. If you are trying to determine the generic parameter types of a generic class you can do the likes of this:
    vb.net Code:
    1. Dim list1 As New List(Of String)
    2. Dim list2 As New List(Of Form)
    3. Dim list3 As New List(Of DataTable)
    4.  
    5. For Each t As Type In list1.GetType().GetGenericArguments()
    6.     MessageBox.Show(t.ToString(), "list1")
    7. Next t
    8.  
    9. For Each t As Type In list2.GetType().GetGenericArguments()
    10.     MessageBox.Show(t.ToString(), "list2")
    11. Next t
    12.  
    13. For Each t As Type In list3.GetType().GetGenericArguments()
    14.     MessageBox.Show(t.ToString(), "list3")
    15. Next t
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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