c# - Get pointer value via reflection -
i have instance of type object
, know pointer (can verified myobject.gettype().ispointer
). possible obtain pointer's value via reflection?
code far:
object obj = .... ; // type , value unknown @ compile time type t = obj.gettype(); if (t.ispointer) { void* ptr = pointer.unbox(obj); // can obtain (the object's) bytes with: byte[] buffer = new byte[marshal.sizeof(t)]; marshal.copy((intptr)ptr, buffer, 0, buffer.length); // how can value represented byte array 'buffer'? // or how can value of *ptr? // following line doesn't work: object val = (object)*ptr; // error cs0242 (obviously) }
addendum №1: object in question value type -not reference type-, cannot use
gchandle::fromintptr(intptr)
followed gchandle::target
obtain object's value...
i suppose need ptrtostructure. this:
if (t.ispointer) { var ptr = pointer.unbox(obj); // following line edited op ;) var underlyingtype = t.getelementtype(); var value = marshal.ptrtostructure((intptr)ptr, underlyingtype); }
Comments
Post a Comment