【发布时间】:2014-06-26 12:38:45
【问题描述】:
我有一个通用扩展方法,如果一个控件匹配多个接口,它就可用。我可以直接在支持这两个接口的控件上调用此方法...
Public Function DisplayValue(Of T As {IChoiceControl, IDataSourceControl})(ByVal vctl As T) As String
End Function
Dim pctl As CustomControl = ... //implements above interfaces
Dim pstrDisplayed As String = pctl.DisplayValue() // works
...但是如何转换一个我确定匹配两个接口的对象,以便我可以调用该方法?
Dim pobj As Object = ...
If TypeOf pobj Is IChoiceControl AndAlso TypeOf pobj Is IDataSourceControl Then
pstrDisplayed = CType(pobj, ?).DisplayValue()
End If
编辑:
定义复合接口(如图所示)工作正常。谢谢。
Public Interface IChoiceDataSourceControl
Inherits IChoiceControl, IDataSourceControl
End Interface
If TypeOf pobj Is IChoiceDataSourceControl Then
pstrDisplayed = CType(pobj, IChoiceDataSourceControl).DisplayValue()
End If
【问题讨论】:
标签: c# .net vb.net generics interface