Can you pass a "type" as an argument?

I want to do something like VB.NET, is this possible?

Function task(value as Object, toType as Type)

   Return DirectCast(value, toType)

End Function
+5
source share
4 answers

Yes. There is System.Type. You might really want to make Generic.

Function SomeFunction(Of T)(obj As Object) As T
    '' Magic
End Function
+12
source

you want to use

function task(of myType)(value as myType) as MyType
   ''stuff
   return value
end function
+2
source

. , :

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

:

BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
+2

, , , CType / . CType , , DirectCast , value toType.

0

All Articles