Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Create a list of Unknown type at compile time

Create a list of Unknown type at compile time

Scheduled Pinned Locked Moved Visual Basic
csharpdebuggingregexquestion
12 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dilkonika

    Hello ! It's possible to create a list of unknown type at compile time ? Let's suppose the type is in one variable as a string. And I want to create a list of this type , and of course after I want to add items from this type to the list. I have found this code on C# , but it seems not work on vb.net :

    // Create a List<> of unknown type at compile time.
    Type customList = typeof(List<>).MakeGenericType(tempType);
    IList objectList = (IList)Activator.CreateInstance(customList);

    // Copy items from a PropertyInfo list to the object just created
    object o = objectThatContainsListToCopyFrom;
    PropertyInfo p = o.GetType().GetProperty("PropertyName");
    IEnumerable copyFrom = p.GetValue(o, null);
    foreach(object item in copyFrom) objectList.Add(item); // Will throw exceptions if the types don't match.

    // Iterate and access the items of "objectList"
    // (objectList declared above as non-generic IEnumerable)
    foreach(object item in objectList) { Debug.WriteLine(item.ToString()); }

    I just want to create the list , and after to add items like this :

    List1.Items.Add(object).

    What can I do ? Thank you !

    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #2

    That looks like it should work in C#. What problem do you have with the VB-translation?

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    D 1 Reply Last reply
    0
    • S Sascha Lefevre

      That looks like it should work in C#. What problem do you have with the VB-translation?

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #3

      I don't know c# , so I have used a code translator. this is the code :

      Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
      Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)

      ' Copy items from a PropertyInfo list to the object just created
      Dim o As Object = objectThatContainsListToCopyFrom
      Dim p As PropertyInfo = o.[GetType]().GetProperty("PropertyName")
      Dim copyFrom As IEnumerable = p.GetValue(o, Nothing)
      For Each item As Object In copyFrom
      objectList.Add(item)
      Next
      ' Will throw exceptions if the types don't match.
      ' Iterate and access the items of "objectList"
      ' (objectList declared above as non-generic IEnumerable)
      For Each item As Object In objectList
      Debug.WriteLine(item.ToString())
      Next

      It shows several errors , but the first is on second line :

      Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'

      S 1 Reply Last reply
      0
      • D dilkonika

        I don't know c# , so I have used a code translator. this is the code :

        Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
        Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)

        ' Copy items from a PropertyInfo list to the object just created
        Dim o As Object = objectThatContainsListToCopyFrom
        Dim p As PropertyInfo = o.[GetType]().GetProperty("PropertyName")
        Dim copyFrom As IEnumerable = p.GetValue(o, Nothing)
        For Each item As Object In copyFrom
        objectList.Add(item)
        Next
        ' Will throw exceptions if the types don't match.
        ' Iterate and access the items of "objectList"
        ' (objectList declared above as non-generic IEnumerable)
        For Each item As Object In objectList
        Debug.WriteLine(item.ToString())
        Next

        It shows several errors , but the first is on second line :

        Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #4

        This works for me:

        Option Explicit On
        Option Strict On

        Imports System.Reflection

        Module Module1

        Public Class ChildClass
        End Class
        
        Public Class SomeClass
            Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
        End Class
        
        Sub Main()
            Dim tempType = GetType(ChildClass)
            Dim someClass = New SomeClass
            someClass.Children.Add(New ChildClass)
        
            Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
            Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)
        
            ' Copy items from a PropertyInfo list to the object just created
            Dim o As Object = someClass
            Dim p As PropertyInfo = o.\[GetType\]().GetProperty("Children")
            Dim copyFrom As IEnumerable = CType(p.GetValue(o, Nothing), IEnumerable)
            For Each item As Object In copyFrom
                objectList.Add(item)
            Next
            ' Will throw exceptions if the types don't match.
            ' Iterate and access the items of "objectList"
            ' (objectList declared above as non-generic IEnumerable)
            For Each item As Object In objectList
                Debug.WriteLine(item.ToString())
            Next
        End Sub
        

        End Module

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        D 1 Reply Last reply
        0
        • S Sascha Lefevre

          This works for me:

          Option Explicit On
          Option Strict On

          Imports System.Reflection

          Module Module1

          Public Class ChildClass
          End Class
          
          Public Class SomeClass
              Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
          End Class
          
          Sub Main()
              Dim tempType = GetType(ChildClass)
              Dim someClass = New SomeClass
              someClass.Children.Add(New ChildClass)
          
              Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
              Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)
          
              ' Copy items from a PropertyInfo list to the object just created
              Dim o As Object = someClass
              Dim p As PropertyInfo = o.\[GetType\]().GetProperty("Children")
              Dim copyFrom As IEnumerable = CType(p.GetValue(o, Nothing), IEnumerable)
              For Each item As Object In copyFrom
                  objectList.Add(item)
              Next
              ' Will throw exceptions if the types don't match.
              ' Iterate and access the items of "objectList"
              ' (objectList declared above as non-generic IEnumerable)
              For Each item As Object In objectList
                  Debug.WriteLine(item.ToString())
              Next
          End Sub
          

          End Module

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          D Offline
          D Offline
          dilkonika
          wrote on last edited by
          #5

          sorry , but the Ilist type is unknown . Everytime I get an error that :

          Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'

          I don't know why ?

          S 1 Reply Last reply
          0
          • D dilkonika

            sorry , but the Ilist type is unknown . Everytime I get an error that :

            Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'

            I don't know why ?

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #6

            Do you get this error also when running my code on its own in a new console-project?

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            D 2 Replies Last reply
            0
            • S Sascha Lefevre

              Do you get this error also when running my code on its own in a new console-project?

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              D Offline
              D Offline
              dilkonika
              wrote on last edited by
              #7

              Yes , Ilist and Ienumerable are unknown and produce a similar error message that I have posted before.

              S 1 Reply Last reply
              0
              • D dilkonika

                Yes , Ilist and Ienumerable are unknown and produce a similar error message that I have posted before.

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #8

                I wouldn't know why it should be neccessary for you, as it works for me without, but try it with these imports:

                Imports System.Collections
                Imports System.Collections.Generic

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                1 Reply Last reply
                0
                • S Sascha Lefevre

                  Do you get this error also when running my code on its own in a new console-project?

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  D Offline
                  D Offline
                  dilkonika
                  wrote on last edited by
                  #9

                  I add :

                  Imports System.Collections

                  and now the errors are resolved. But I have some problems in runtime 1. I want first to initialize an empty list. Because if I have just the :

                  dim itemlist as Ilist

                  ( Before calling these 2 lines

                  Dim customList As Type = GetType(List(Of )).MakeGenericType(GetType(MyClass))
                  itemlist = CType(Activator.CreateInstance(customList), IList)

                  How can I check if the list is empty or ?? something similar. But I want a general checkin mode , because the same expression must be correct even when I clear the list and I want to check if empty ( before initializing for a new type ). ( I try to use

                  If itemlist.Count > 0 Then

                  but doesn't work.

                  S 1 Reply Last reply
                  0
                  • D dilkonika

                    I add :

                    Imports System.Collections

                    and now the errors are resolved. But I have some problems in runtime 1. I want first to initialize an empty list. Because if I have just the :

                    dim itemlist as Ilist

                    ( Before calling these 2 lines

                    Dim customList As Type = GetType(List(Of )).MakeGenericType(GetType(MyClass))
                    itemlist = CType(Activator.CreateInstance(customList), IList)

                    How can I check if the list is empty or ?? something similar. But I want a general checkin mode , because the same expression must be correct even when I clear the list and I want to check if empty ( before initializing for a new type ). ( I try to use

                    If itemlist.Count > 0 Then

                    but doesn't work.

                    S Offline
                    S Offline
                    Sascha Lefevre
                    wrote on last edited by
                    #10

                    I have a hard time understanding that - can you explain it a bit more detailed?

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    D 1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      I have a hard time understanding that - can you explain it a bit more detailed?

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      D Offline
                      D Offline
                      dilkonika
                      wrote on last edited by
                      #11

                      ok. I need a way to detect that this list is empty. But the expression that should do this , must include 2 cases : - The list is not yet created . ( So it's just declared as mylist as Ilist). - The list is created ( with your code ) but the items are deleted ( or has no items ). I try with the code : If mylist.Count..... but this doesn't work.

                      S 1 Reply Last reply
                      0
                      • D dilkonika

                        ok. I need a way to detect that this list is empty. But the expression that should do this , must include 2 cases : - The list is not yet created . ( So it's just declared as mylist as Ilist). - The list is created ( with your code ) but the items are deleted ( or has no items ). I try with the code : If mylist.Count..... but this doesn't work.

                        S Offline
                        S Offline
                        Sascha Lefevre
                        wrote on last edited by
                        #12

                        If mylist IsNot Nothing AndAlso mylist.Count > 0 Then
                        End If

                        ?

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups