I wrote this example to help explain. As you can see I have an object hierarchy. I'd like to modify the GetFeatures() function to only return the features added by constructor of object type I instantiated. For example, BasicModel.GetFeatures(new LuxuryModel()) should only return the features "Leather Seats" and "Sunroof". I don't mind using reflection if I have to.
Public Class Feature
Public Sub New(ByVal model As BasicModel, ByVal description As String)
_model = model
_description = description
End Sub
Private _model As BasicModel
Public Property Model() As BasicModel
Get
Return _model
End Get
Set(ByVal value As BasicModel)
_model = value
End Set
End Property
Private _description As String
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
End Class
Public Class BasicModel
Public Sub New()
_features = New List(Of Feature)
End Sub
Private _features As List(Of Feature)
Public ReadOnly Property Features() As List(Of Feature)
Get
Return _features
End Get
End Property
Public Shared Function GetFeatures(ByVal model As BasicModel) As List(Of Feature)
'I know this is wrong, but something like this...'
Return model.Features.FindAll(Function(f) f.Model.GetType() Is model.GetType())
End Function
End Class
Public Class SedanModel
Inherits BasicModel
Public Sub New()
MyBase.New()
Features.Add(New Feature(Me, "Fuzzy Dice"))
Features.Add(New Feature(Me, "Tree Air Freshener"))
End Sub
End Class
Public Class LuxuryModel
Inherits SedanModel
Public Sub New()
MyBase.New()
Features.Add(New Feature(Me, "Leather Seats"))
Features.Add(New Feature(Me, "Sunroof"))
End Sub
End Class