2017-10-17 4 views
0

Comment puis-je tester si un type de classe générique implémente une interface spécifique?Inspect Genric Type

échantillon

:

Public Interface IBaseItemInterface(of Type) 
    ... 
    End Interface 

    Public Interface ISpecificItemInterface(Of Type) 
     Inherits IBaseItemInterface(of Type) 
    ... 
    End Interface 

    Public Interface IRootInterface(of Type, TEntity as IBaseItemInterface)) 
    ... 
    End Interface 

    'Implementation 
    Public Class Sample(of Type, TEntity as IBaseItemInterface)) 
     Implements IRootInterface(of Type, TEntity) 

     Public Sub test() 
      **If TEntity Implements ISpecificItemInterface then** 
       DoSomethingSpecific 
      End if 
     End Sub 
    End Class 

Comment cela peut-il être fait? Y a-t-il une autre façon de faire cela?

Répondre

0

Vous pouvez utiliser ...

Public Sub Test() 
    Dim type = GetType(TEntity) 
    Dim isMySpecificInterface = type.IsGenericType AndAlso 
      type.GetGenericTypeDefinition() Is GetType(ISpecificItemInterface(Of)) 

    If isMySpecificInterface then 
     ' DoSomethingSpecific 
    End if 
End Sub 
+0

Merci beaucoup :) –