我有BaseAbstractClass(of T as WebControl)
(VB泛型),它繼承WebControl
。將繼承的泛型返回爲基類型
BaseAbstractClass
由ConcreteWrapper1
,ConcreteWrapper2
繼承,最後,稍微改動一下,ConcreteWrapper4
。其中每一個將繼承BaseAbstractClass
使用從WebControl
繼承的不同的類。
我想要做的是有工廠返回ConcreteWrapper
作爲BaseAbstractClass(of WebControl)
。但每當我嘗試返回ConcreteWrapper
的新實例時,我都會收到編譯時轉換錯誤。
[編輯:代碼添加]
BaseAbstractClass
Public MustInherit Class BaseAbstractClass(Of T As WebControl)
Inherits WebControl
Protected _item As T
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
_item.RenderControl(writer)
End Sub
End Class
其他ConcreteWrappers這個樣子,除了與不同CustomControl
Public Class ConcreteWrapper1
Inherits BaseAbstractClass(Of CustomControlInheritedFromWebControl1)
Public Sub New(ByVal control As CustomControlInheritedFromWebControl1)
MyBase._item = control
End Sub
End Class
Public Class CustomControlInheritedFromWebControl1
Inherits WebControl
//not the correct comment markers but the coloring works better
//do stuff here... Implm not important.
End Class
我廠
Public Class WebControlFactory
Public Shared Function GetWebControl() As BaseAbstractClass(Of WebControl)
Return New ConcreteWrapper1(New CustomControlInheritedFromWebControl1())
End Function
End Class
[/編輯]
我可以解釋發生了什麼,爲什麼不起作用(也可能是解決方案)?
謝謝!
發佈代碼段會非常有幫助! – 2009-01-26 19:52:32