我发现自己需要在 HtmlControls和WebControls上执行相同的操作.我坚信DRY并且发现如果我想在两种类型上合并函数,那么只有Control类才能使用.我使用Control时遇到的问题是HtmlControl和WebControl都
Private Sub SetAttribute(ByRef ctrl As Control, ByVal key As String, ByVal value As String) If TypeOf ctrl Is HtmlControl Then DirectCast(ctrl, HtmlControl).Attributes(key) = value ElseIf TypeOf ctrl Is WebControl Then DirectCast(ctrl, WebControl).Attributes(key) = value End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load For Each ctrl In Me.Controls SetAttribute(ctrl, "class", "classname") Next End Sub