我收到编译错误类型“ctltype”未使用此代码定义。
这是.NET 1.1遗留代码,所以我不知道。
任何人都知道为什么??
Public Function GetControlText(ByVal ctls As Control, ByVal ctlname As String, ByVal ctltype As Type) As String
Dim ctl As Control
Dim res As String
ctl = ctls.FindControl(ctlname)
If ctl Is Nothing Then
Return ""
End If
res = CType(ctl, ctltype).Text
If res Is Nothing Then
Return ""
Else
Return res
End If
End Function
答案 0 :(得分:2)
CType
的第二个操作数必须是类型名称 - 而不是类型为Type
的变量。换句话说,必须在编译时知道类型。
在这种情况下,你想要的只是Text
属性 - 你可以用反射得到它:
Public Function GetControlText(ByVal ctls As Control, ByVal ctlname As String, _
ByVal ctltype As Type) As String
Dim ctl As Control = ctls.FindControl(ctlname)
If ctl Is Nothing Then
Return ""
End If
Dim propInfo As PropertyInfo = ctl.GetType().GetProperty("Text")
If propInfo Is Nothing Then
Return ""
End If
Dim res As String = propInfo.GetValue(propInfo, Nothing)
If res Is Nothing Then
Return ""
End If
Return res
End Function