VB.Net - how can I get the type of an object contained in a list

If I have a list ...

dim l as List(of MyClass) = new List(of MyClass)

and I want to get the type of objects contained in the list, how to do it?

The obvious answer, which seems impossible from my actual implementation, would be to do something like this ...

public function GetType(byval AList as IList(of GenericType)) as System.Type
  dim lResult as system.type = nothing
  if AList.Count > 0 then lResult = AList(0).GetType
  return lResult
end function

But what if the list is empty and I still want to know the type it contains?

+3
source share
1 answer

There's a nice article about it on MSDN, here

GetGenericArguments(), , . , , , ,

dim l as List(of MyClass) = new List(of MyClass)
dim t as Type = (l.GetGenericArguments())(0)
+3

Source: https://habr.com/ru/post/1759231/


All Articles