simplezz Posted July 24, 2015 Share Posted July 24, 2015 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 More sharing options...
0 Andre S. Veteran Posted July 25, 2015 Veteran Share Posted July 25, 2015 A common technique to get around the limitations of generic constrains is to parameterize the non-generic functions you want to use. So: private void writePrimitive<T>(Func<T, byte[]> getBytes, T obj) { byte[] bytes = getBytes(obj); Buffer.BlockCopy(bytes, 0, buf, pos, bytes.Length); update(bytes.Length); } // Usage: writePrimitive(BitConverter.GetBytes, 3); writePrimitive(BitConverter.GetBytes, true); Can't get around having to repeat BitConverter.GetBytes, but that's about as good as it gets I think. Type inference figures out what overload to call based on the type of the second parameter. Link to comment https://www.neowin.net/forum/topic/1266054-bufferblockcopy-passing-generic-argument/#findComment-596940364 Share on other sites More sharing options...
0 simplezz Posted July 28, 2015 Author Share Posted July 28, 2015 On 25/07/2015 at 02:22, Andre S. said: A common technique to get around the limitations of generic constrains is to parameterize the non-generic functions you want to use. So: private void writePrimitive<T>(Func<T, byte[]> getBytes, T obj) { byte[] bytes = getBytes(obj); Buffer.BlockCopy(bytes, 0, buf, pos, bytes.Length); update(bytes.Length); } // Usage: writePrimitive(BitConverter.GetBytes, 3); writePrimitive(BitConverter.GetBytes, true); Can't get around having to repeat BitConverter.GetBytes, but that's about as good as it gets I think. Type inference figures out what overload to call based on the type of the second parameter. Perfect. Thanks Andre. A delegate also worked for the read back function. Link to comment https://www.neowin.net/forum/topic/1266054-bufferblockcopy-passing-generic-argument/#findComment-596942404 Share on other sites More sharing options...
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:
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