Is it possible to get a pointer to a boxed unmanaged value type, without writing a large switch statement that does a cast for every supported type? Something like:
object val; // Contains a boxed unmanaged value, such as int, long, byte, etc.
void* ptr = &val; // Obviously does not compile
DoStuffWithPointer(ptr);
instead of
object val; // Contains a boxed unmanaged value, such as int, long, byte etc.
if (val.GetType() == typeof(byte))
{
var typedValue = (byte)value;
DoStuffWithPointer(&typedValue);
}
else if (val.GetType() == typeof(short))
{
var typedValue = (short)value;
DoStuffWithPointer(&typedValue);
}
else
// ... and so on
Btw. I know the pointer points to a local variable on the stack, and will become invalid once the function returns. That's fine.
Unsafe.AsPointer<T>. Should do what you're looking for.