Create a list of Unknown type at compile time
-
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 !
-
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 !
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
-
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
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())
NextIt shows several errors , but the first is on second line :
Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'
-
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())
NextIt shows several errors , but the first is on second line :
Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'
This works for me:
Option Explicit On
Option Strict OnImports 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
-
This works for me:
Option Explicit On
Option Strict OnImports 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
-
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 ?
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
-
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
-
Yes , Ilist and Ienumerable are unknown and produce a similar error message that I have posted before.
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.GenericIf the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
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
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.
-
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.
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
-
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
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.
-
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.
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