최근에 구현 한 일부 사용자 정의 사용자 정의 컨트롤에 인터페이스를 추가했습니다. 인터페이스는 꽤 기본입니다.제네릭, 인터페이스 및 캐스팅 관련 문제
Public Interface IMyInterface(Of T As WebControl)
Function DoSomething() As T
End Interface
구현도 매우 기본적인 :
Public Class MyCustomControl
Inherits CompositeControl
Implements IMyInterface(Of MyCustomControl)
Public Function DoSomething() As MyCustomControl _
Implements IMyInterface(Of MyCustomControl).DoSomething
' do stuff
Return Me
End Class
모든이 시점까지 잘 작동이 체인을 지원 한 가지 방법이있다.
Dim myList = New List(Of IMyInterface(Of WebControl))
myList.Add(someCustomControl)
myList.ForEach(Sub(i) i.DoSomething())
someCustomControl
이 IMyInterface(Of MyCustomControl)
대신 IMyInterface(Of WebControl)
구현하는 MyCustomControl
이다 : 나는 모든 너무처럼 IMyInterface
인터페이스를 구현하는 컨트롤의 컬렉션을 통해 루프를 시도 할 때 문제가 발생한다.
옵션 엄격한에이 '(WebControl의) IMyInterface'에 'MyCustomControl'에서 암시 적으로 변환 할 수 없습니다 : (나는
someCustomControl
을 추가하려고 할 경우)나는 두 번째 줄에이 오류를 얻고있다.
이 오류를 해결할 수있는 방법이 있습니까? 나는 그 일을하는 것에 가깝지만이 점을 넘어서기 위해서는 제네릭에 대해 충분히 모른다.
'Dim myList = 새 목록 (Of WebControl)'과 같이 myList를 선언하려고 시도하는 동안 대신 'Dim myList = 새 목록 (Of Object)'또는 그와 비슷한 것을 사용해보십시오. – Vishal
@Vishal'IMyInterface' 타입을 가지지 않고서도'ForEach' 루프에있는 객체들에 대해'DoSomething()'을 호출 할 수 없다고 생각합니다. – jbabey
미안하지만, 나는 그것을 몰랐다. – Vishal