I'm trying to pass a value constrained type to BitConverter.GetBytes, but c# only permits a struct constraint. For example:
private void writePrimitive<T>(T obj) where T : struct {
if (!typeof(T).IsPrimitive)
throw new ArgumentException("Only primitive types allowed");
Buffer.BlockCopy(BitConverter.GetBytes(obj), 0, buf, pos, sizeof(obj));
update(sizeof(obj));
}
The compiler understandably borks at this. Still I'd like to find a solution that avoids repetition and doesn't require writing multiple overloads for different primitives.
Completely irrelevant as long as pricing is garbage.
With OneDrive I get storage for about $18 / TB for a year.
With Proton it's $96.
For the record, Google One is around $48, Dropbox $69, iCloud $60.
In other words, Proton is the worst value BY FAR.
Not to mention with OneDrive I get full desktop and web office suites for 6 people.
Question
simplezz
Hi all,
I'm trying to pass a value constrained type to BitConverter.GetBytes, but c# only permits a struct constraint. For example:
private void writePrimitive<T>(T obj) where T : struct { if (!typeof(T).IsPrimitive) throw new ArgumentException("Only primitive types allowed"); Buffer.BlockCopy(BitConverter.GetBytes(obj), 0, buf, pos, sizeof(obj)); update(sizeof(obj)); }The compiler understandably borks at this. Still I'd like to find a solution that avoids repetition and doesn't require writing multiple overloads for different primitives.Link to comment
https://www.neowin.net/forum/topic/1266054-bufferblockcopy-passing-generic-argument/Share on other sites
2 answers to this question
Recommended Posts