I have an instance of a type objectfrom which I know this is a pointer (can be easily checked with myobject.GetType().IsPointer). Is it possible to get the value of a pointer through reflection?
:
object obj = .... ;
Type t = obj.GetType();
if (t.IsPointer)
{
void* ptr = Pointer.Unbox(obj);
byte[] buffer = new byte[Marshal.SizeOf(t)];
Marshal.Copy((IntPtr)ptr, buffer, 0, buffer.Length);
object val = (object)*ptr;
}
<h / "> Appendix No. 1: Since the object is a value type - not a reference type, I cannot use GCHandle::FromIntPtr(IntPtr)then GCHandle::Targetto get the value of the object ...
source
share