0
我在我们的库中有一个用户控件,我需要继承并更新它。我现在遇到的问题是我无法直接实例化新的用户控件。我必须调用库中的一个方法来创建并传递用户控件的一个实例。请检查下面的示例代码。继承自定义控件的问题
我试图使用转换,我得到了InvalidCastException。我认为这是因为第二个需要比第一个更多的存储空间。
在此先感谢您的帮助。
Namespace ProjectA.Components
Public Class MainClass
Public Function CreateCustomControl() As CustomControl
Dim cc As CustomControl = Activator.CreateInstance(Of CustomControl)()
Return cc
End Function
End Class
Public Class CustomControl
Inherits System.Windows.Forms.UserControl
End Class
End Namespace
Namespace ProjectB
Public Class ExtendedCustomControl
Inherits ProjectA.Components.CustomControl
End Class
Public Class MainForm
Inherits System.Windows.Forms.Form
Private Sub CreateInstance()
Dim i As New ProjectA.Components.MainClass
Dim myControl As ExtendedCustomControl = i.CreateCustomControl
' InvalidCastException is thrown.
End Sub
End Class
End Namespace