2016-09-28 53 views
0

我有一段簡單的代碼如下:VB.NET無法轉換類接口

Public Interface ITest(Of T) 
    Function getObj() As T 
End Interface 

Public Class clsTest(Of T As {IOrder, New}) 
    Implements ITest(Of T) 
    Public Function getObj() As T Implements ITest(Of T).getObj 
     Return New T 
    End Function 
End Class 

Public Interface IOrder 
    Property Name As String 
End Interface 

Public Class clsOrder 
    Implements IOrder 
    Public Property Name As String Implements IOrder.Name 
End Class 

當我創建另clsTest對象,如:

Dim test As ITest(Of IOrder) = New clsTest(Of clsOrder) 

我得到了以下錯誤:

'ConsoleApplication1.clsTest(Of ConsoleApplication1.clsOrder)' cannot be converted to 'ConsoleApplication1.ITest(Of ConsoleApplication1.IOrder)'. Consider changing the 'T' in the definition of 'Interface ITest(Of T)' to an Out type parameter, 'Out T'.

任何人都知道原因?如果我確實要將測試作爲ITest(IOrder)申報,我該如何使其工作?謝謝。

+0

謝謝,Tim。它解決了我的問題。 – FrankS

回答

0

製作接口協在TOut-keyword

Public Interface ITest(Of Out T) 
    Function getObj() As T 
End Interface 

,那麼這將編譯:

Dim test As ITest(Of IOrder) = New clsTest(Of clsOrder) 

E.利珀特可以解釋它比我好:

相關問題