Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:07:45+00:00 2026-05-24T01:07:45+00:00

So I know there is this question addressing the need for getting the bytes

  • 0

So I know there is this question addressing the need for getting the bytes of an object. But I am wondering if there is an approach to calling BitConverter.GetBytes on a generic type where I know the type is a primitive (Int32, UInt16, etc).

Because people love silly examples as a prerequisite to actually answering questions here:

Public Sub Foobar(Of T as Structure)()
    Dim x as T 'Assume T is declared as Int32
    Dim y() as Byte

    y = System.BitConverter.GetBytes(x)
End Sub

The above will throw your usual error:

Overload resolution failed because no accessible 'GetBytes' can be called with these arguments:
    'Public Shared Function GetBytes(value As Double) As Byte()': Value of type 'T' cannot be converted to 'Double'.
    'Public Shared Function GetBytes(value As Single) As Byte()': Value of type 'T' cannot be converted to 'Single'.
    'Public Shared Function GetBytes(value As ULong) As Byte()': Value of type 'T' cannot be converted to 'ULong'.
    'Public Shared Function GetBytes(value As UInteger) As Byte()': Value of type 'T' cannot be converted to 'UInteger'.
    'Public Shared Function GetBytes(value As UShort) As Byte()': Value of type 'T' cannot be converted to 'UShort'.
    'Public Shared Function GetBytes(value As Long) As Byte()': Value of type 'T' cannot be converted to 'Long'.
    'Public Shared Function GetBytes(value As Integer) As Byte()': Value of type 'T' cannot be converted to 'Integer'.
    'Public Shared Function GetBytes(value As Short) As Byte()': Value of type 'T' cannot be converted to 'Short'.
    'Public Shared Function GetBytes(value As Char) As Byte()': Value of type 'T' cannot be converted to 'Char'.
    'Public Shared Function GetBytes(value As Boolean) As Byte()': Value of type 'T' cannot be converted to 'Boolean'.

One solution I think would work is a large Select Case calling GetType(), but that is horrendously slow (because of boxing) and looks ugly. I would think that since I call my higher level class with a primitive data type for T, that the compiler would be smart enough to figure it out, but I assume I am not providing enough information for it to derive what T’s underlying value is at compile time for the invoked instances.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:07:45+00:00Added an answer on May 24, 2026 at 1:07 am

    Solved it myself.

    Using a stripped-down version of the solution provided in my question, “Faster way to convert from a String to generic type T when T is a valuetype?”, I was able to to create an extension method to a generic type called GenericGetBytes that would use the same delegate tricks to access the appropriate GetBytes method in the System.BitConverter class. This would preserve a decent amount of performance without me having to write up a bunch of overloaded functions for each type.

    Code:

    <System.Runtime.CompilerServices.Extension()>
    Friend Function GenericGetBytes(Of T As Structure)(ByVal value As T) As Byte()
        Return GenericBitConverter(Of T).GetBytes(value)
    End Function
    
    Private Class GenericBitConverter(Of T As Structure)
        Private Sub New()
        End Sub
    
        Friend Delegate Function GetBytesFunc(ByVal value As T) As Byte()
        Friend Shared ReadOnly GetBytes As GetBytesFunc = FetchGetBytesFunc()
    
        Private Shared Function FetchGetBytesFunc() As GetBytesFunc
            Return DirectCast(GetBytesFunc.CreateDelegate(GetType(GetBytesFunc),
                                    GetType(BitConverter).GetMethod("GetBytes", New Type() {GetType(T)})), 
                              GetBytesFunc)
        End Function
    End Class
    

    Invocation:

    Public Sub Foobar(Of T As Structure)(ByVal value As T)
        Debug.Print(BitConverter.ToString(value.GenericGetBytes))
    End Sub
    
    Call Foobar(Of Int16)(42)
    

    Output:

    2A-00
    

    Edit: And then I discover that the CType operator cannot convert from a base class to a derived class, which means the entire goal of this question and answer just became moot, as I have to move all my CType operators back to the derived classes. Hah.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's no one answer to this question, but I'd like to get
I know there is already a question about this but my issue is more
I know there is no such thing as a dumb question but this is:
I'm almost certain I know the answer to this question, but I'm hoping there's
This is a very narrow and specific question, but I know there are someone
There's already a question on this here. But i want to know if its
I know this is a simple question for someone out there, but I have
I know this is a bit of a newbie question, but are there equivalents
I know there is this question that is very similar, but I wanted to
I know there are many variations of this question posted, but none I've found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.