Generic WithEvents

时间:2010-03-09 11:39:18

标签: .net vb.net

错误:

  

'WithEvents'变量只能是   键入为 类,接口或类型   具有类约束的参数

背景:

Public Class Tadpole(Of T As IVisibleChanged, P As IVisibleChanged)
  Private WithEvents _Tad As T ' ERROR '
  Private WithEvents _Pole As P ' ERROR '

  Public Property Tad() As T ...

  Public Property Pole() As P ...

End Class

''' IVisibleChanged '''
Public Interface IVisibleChanged
  Property Visible() As Boolean
  Event VisibleChanged As EventHandler
End Interface

解决方法:

一个。使用AddHandler处理结构中定义的事件。

修改

湾使用Private WithEvents _Tad As IVisibleChanged (M.A。Hanin)

℃。 ?

1 个答案:

答案 0 :(得分:3)

我怀疑这是因为WithEvents不支持值类型。当您仅将T限制为IVisibleChanged时,您不保证引用类型,因此不能使用WithEvents。我不知道VB语法,但如果它像C#那样你可能会这样做:

' Not sure of the VB syntax.
(Of T As {IVisibleChanged, Class})

这保证了T不仅会实现IVisibleChanged,它也不会是一个结构体。