How do I use GetTypeArray?

  • Thread starter Thread starter Benjamin Lukner
  • Start date Start date
B

Benjamin Lukner

Hi!

I'm just building a wrapper for a dll that has a Write() function that
is overloaded 13 times for all the different data types.

I thought it would be easier on my side to offer a function that accepts
"Object" as parameter. It works fine for regular variables but not for
arrays. The following code works, but I think it would be neater if
GetTypeArray would be used.


Sub TestCaller()
Dim Test(1) As Byte
WriteToFile(0, Test) ' Separates between e.g. "Byte" and "Byte()"
End Sub


Sub WriteToFile(ByVal Offset As Int32, ByVal Data As Object) As Boolean

'[...]

Select Case Type.GetTypeCode(Data.GetType)

Case TypeCode.Boolean, TypeCode.Byte, TypeCode.String
status = oFile.Write(Data)

Case Else ' Type = Object
If Data.GetType.IsArray _
AndAlso Data.GetType.Name() = "Byte[]" Then ' !Line to replace!
Dim aData() As Byte = Data
status = oFile.Write(aData, 0, aData.Length)
End If

End Select

End Sub


I tried "Dim aType() As Type = Type.GetTypeArray(Data)", but that throws
an invalid cast exception. I think the problem is that the type is
"Data" and not "Data()"...?

Does anyone know if it is possible to use GetTypeArray here?

Kind regards,

Benjamin Lukner
 
Once you have got a type of array you can get its element type by using
GetElementType() function:

....AndAlso Type.GetTypeCode(Data.GetType.GetElementType()) =
TypeCode.Byte Then
Dim aData() As Byte = Data
End If


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Sergey said:
Once you have got a type of array you can get its element type by using
GetElementType() function:

...AndAlso Type.GetTypeCode(Data.GetType.GetElementType()) =
TypeCode.Byte Then
Dim aData() As Byte = Data
End If

Thanks, works great :-)

Kind regards,

Benjamin Lukner
 
Back
Top