請參閱下面的代碼:反思 - 從一個字符串值,創建數據類型
Public Function Test()
Dim o As Object = getVariable("Integer")
If TypeOf o Is Integer Then
'Do some processing on the integer
ElseIf TypeOf o Is Decimal Then
'Do some processing on the integer
End If
End Function
Public Function getVariable(ByVal strDataType As String)
If strDataType = "Integer" Then
Return New Integer
ElseIf strDataType = "Decimal" Then
Return New Decimal
ElseIf strDataType = "Double" Then
Return New Double
End If
End Function
我懷疑有與反思這樣做的一個簡單的方法(更少的代碼)?
使用[泛型](http://msdn.microsoft.com/en-us/library/4a1b71ta.aspx)而不是反射。 –
並使用'Enum'而不是字符串,並且'VB.NET'是否有'default'關鍵字? – ja72
@ ja72:VB.NET擁有全能的'Nothing'。 :) – Neolisk