SideByEach
SideByEach

Reputation: 31

How do I get all class properties by type?

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

Answers (1)

Tim Schmelter
Tim Schmelter

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

Related Questions