Consider the following extension method:
<Extension()> _
Public Function Satisfies(Of T)(ByVal subject As T, ByVal specification As ISpecification(Of T)) As Boolean
Return specification.IsSatisfiedBy(subject)
End Function
This works as expected if subject is the exact class being operated on by the specification. However, if the specification is examining the super-class of T, this extension will not work unless subject is explicitly cast to the super-class. Is there a way I can avoid this? So far, the best I've been able to come up with is:
<Extension()> _
Public Function Satisfies(Of T As Class, K As Class)(ByVal subject As T, ByVal specification As ISpecification(Of K)) As Boolean
Return specification.IsSatisfiedBy(TryCast(subject, K))
End Function
But I can't help but think there's a better way....
Update:
Since I (apparently) can't get this to work exactly as I'd like in VB.NET due to limitations in the language itself, is my second attempt the safest/most efficient way to do this?