确定泛型类型的长度?

时间:2018-04-18 13:39:06

标签: vb.net generics struct size

如何在运行时确定泛型类型的长度(使用约束Structure)。

我需要字节数来创建MemoryMappedFile的视图访问器(在构造函数中初始化)。

Private goVirtualFile As MemoryMappedFile
Private glLength As Long = 0            

Public Class CTest(Of T As Structure)
    Public Sub Append(tData As T())
        Dim iNumStruct As Integer = tData.Length         'Number of structures.
        Dim iStructLen As Integer = ...                  'Length of 1 structure in bytes.
        Dim iBytes As Integer = iNumStruct * iStructLen  'Total length in bytes.

        Using goViewStream = goVirtualFile.CreateViewAccessor(
            glLength, iBytes)
            ...
        End Using
    End Sub

    Public Function Read(lOffset As Long, iNumStruct As Integer) As T()
        ...
    End Sub
End Class

0 个答案:

没有答案