dnatoli
dnatoli

Reputation: 7012

Matching types using reflection in .NET

I am trying to use reflection to collect a property from a class that returns a certain type. However some of the properties I'm returning are strongly typed lists that house the type I want. Essentially I am trying to do the following:

Public Function GetPropertyInfo(ByVal t as System.Type)
   for each pi as System.Reflection.PropertyInfo in ob.GetType.GetProperties()
      if pi.PropertyType.equals(GetType(List(Of t)))
         return pi
      end if
   next

   Return Nothing
End Function

Obviously this doesn't work as it throws an error saying t is not a declared type. Is there any way to do this?

Thanks.

Upvotes: 0

Views: 195

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99859

In C#, you are looking for this syntax:

Type desiredPropertyType = typeof(List<>).MakeGenericType(new Type[] { t });

Which reflector says is this:

Dim desiredPropertyType As Type = GetType(List(Of )).MakeGenericType(New Type() { t })

Upvotes: 2

Related Questions