Reputation: 31
How can I get all the properties of a class that implement a specific base class or interface?
I have a properties class that contains several other property classes. Some, not all, of these classes implement an interface. I would like to know if it's possible to iterate over all properties of parent class for child-classes that implement the target interface.
It sounds like a job for reflection? I'm just not sure how. Can it be done via the "PropertyInfo" object? Or am I barking up the wrong tree?
Upvotes: 1
Views: 893
Reputation: 460028
You could use IsAssignableFrom
on the PropertyInfo
's PropertyType
:
For Each prop In Me.GetType().GetProperties()
If prop.PropertyType.IsAssignableFrom(GetType(YourInterface)) Then
' do something '
End If
Next
Upvotes: 2