Voglio fare qualcosa di simile a quanto segue in VB.NET, è possibile?Puoi passare un "tipo" come argomento?
Function task(value as Object, toType as Type)
Return DirectCast(value, toType)
End Function
Voglio fare qualcosa di simile a quanto segue in VB.NET, è possibile?Puoi passare un "tipo" come argomento?
Function task(value as Object, toType as Type)
Return DirectCast(value, toType)
End Function
Sì. C'è System.Type. Potresti voler fare un Generic comunque.
Function SomeFunction(Of T)(obj As Object) As T
'' Magic
End Function
Oh doh. Sono così abituato a C# in questi giorni. –
si desidera utilizzare il
function task(of myType)(value as myType) as MyType
''stuff
return value
end function
Sì, anche se, a seconda delle esigenze, si può decidere di utilizzare CType per fare qualsiasi tipo di fusione/conversione. CType funzionerà finché esiste una conversione di tipo valida, mentre DirectCast richiede che value
sia del tipo toType
.
grande risposta - Ecco una funzione generica di fare lo stesso:
Public Sub BindListControlToEnum(Of T)(ListCtrl As ListControl)
Dim itemValues As Array = System.Enum.GetValues(GetType(T))
Dim itemNames As Array = System.Enum.GetNames(GetType(T))
For i As Integer = 0 To itemNames.Length - 1
Dim item As New ListItem(itemNames(i), itemValues(i))
ListCtrl.Items.Add(item)
Next
End Sub
chiamare in questo modo:
BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
Perché non lanciare l'oggetto * prima * di inviarlo al Funzione di attività? – Juliet
Non è una soluzione ovvia ?! –