- I have a value in a variable of type
Object. - I have its type in a variable of type
Type. - Type can be only among standard system types (Int32, Int64, etc..)
Q. I want to get bytes array representing this value. I’m trying to use BitConverter.GetBytes for that, but it requires a typed variable. Is there a way to get typed variable dynamically having a value and type in separate variables?
Thank you.
If you don’t want to
switchon each type and call the appropriate method, which is the fastest way, you could use reflection, albeit a bit slower:Calling
GetBytes((short)12345)producesnew byte[] { 0x39 ,0x30 }.