Generic Method Using BitConverter.GetBytes
<p>I'm trying to get the bytes for different objects of different types using <a href="https://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes(v=vs.90).aspx" rel="nofollow"><code>BitConverter.GetBytes</code></a>. In order to do this, <strong>I'd like to write a generic extension method</strong>, rather than write individual extension methods for every type I want to work with (short, long, ulong, etc).</p>

<p><strong>Is this possible?</strong></p>

<p>Here's the (not working) code I have so far for the generic method:</p>

<pre><code>public static byte[] Foo&lt;T&gt;(this T input) where T : struct
{
    // error here, no match to overloads
    var result = BitConverter.GetBytes(input);
    // do more logic here...
    return result;
}
</code></pre>

<p>By the way, this must be .NET 3.5 compliant.</p>