I'd like to pass a value type to a function and set it to a repeating bit pattern (FF, AA, etc.) across the entire width of the variable. Right now, I'm passing the value with
void foo(T val) where T : struct
so I can use any value type. The problem is, the compiler won't let me use sizeof(T), because T could be a reference type (except that it can't, thanks to the "where" constraint). I could hard code all the value types and check against them myself, but that obviously seems like overkill. Is there a simpler way to do this?
Just to clarify: if I pass a byteInt64
, I want to set it to 0xFFFFFFFF.
I tried Convert.ChangeType(0xFFFFFFFF, typeof(T))
, but it throws if val
is e.g. a Char. I could solve the problem by a) figuring out how wide the type-parameter is and "building" a big-enough value to stuff in, b) figuring out how to accept any value type (and only value types), such that sizeof() would work, or c) figuring out how to automagically truncate 0xFFFFFFFF down to the correct width for the variable.