D
DaveD
Can anyone help me get this compiled ?
void Write<T>(T val) {
byte[] bytes = BitConverter.GetBytes(val);
Array.Reverse(bytes);
writer.Write(bytes);
}
The problem is that for T=bool, BitConverter.GetBytes(bool) won't
compile. In C++ I would have created an explicit instantiation like
this
void Write<bool>( bool val ) { writer.Write((byte)(val?
1:0)); } //
I know that we don't have this capability in C#, so any other ideas
welcome please. I can't see a "where" constraint that will work for
me, unless I create my own wrapper classes (don't go there please!).
Thanks and regards
Dave Dawkins
void Write<T>(T val) {
byte[] bytes = BitConverter.GetBytes(val);
Array.Reverse(bytes);
writer.Write(bytes);
}
The problem is that for T=bool, BitConverter.GetBytes(bool) won't
compile. In C++ I would have created an explicit instantiation like
this
void Write<bool>( bool val ) { writer.Write((byte)(val?
1:0)); } //
I know that we don't have this capability in C#, so any other ideas
welcome please. I can't see a "where" constraint that will work for
me, unless I create my own wrapper classes (don't go there please!).
Thanks and regards
Dave Dawkins