2009-07-21 7 views
0

라이브러리에서 상속 받고 업데이트해야하는 사용자 정의 컨트롤이 있습니다. 지금 당장 가지고있는 문제는 새 사용자 정의 컨트롤을 직접 인스턴스화 할 수 없다는 것입니다. 라이브러리에서 사용자 정의 컨트롤의 인스턴스를 만들고 전달하는 메서드를 호출해야합니다. 아래 샘플 코드를 확인하십시오.상속 된 사용자 정의 컨트롤 관련 문제

캐스팅을 사용해 보았는데 InvalidCastException이 발생했습니다. 나는 두 번째가 첫 번째 것보다 더 많은 저장 공간을 필요로하기 때문에 그렇게 생각한다.

도움을 주셔서 미리 감사드립니다. 당신이 ExtendedCustomControl를 인스턴스화되지 않습니다 때문입니다

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 

답변

0

, 당신은 CustomControl를 인스턴스화된다. Activator.CreateObject는 기본 클래스를 만드는 것입니다. 실제로 캐스팅 할 클래스의 것이 아니면 무언가를 업 캐스팅 할 수 없습니다.

아마도 당신은 CreateCustomControl()이 System.Type을 가져 와서 Activator.CreateInstance에 대신 전달하기를 원할 것입니다. 그렇게하면 원하는 것을 만들 수 있습니다.

관련 문제