public static void CopyShallow(object source, object target) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } Type t = source.GetType(); if (target.GetType() != t) { throw new ArgumentException("Object types must be equal."); } byte[] data = new byte[UnsafeTools.BaseInstanceSizeOf(t) - IntPtr.Size]; InteropTools.Pin( source, o1 => { var ptr = UnsafeTools.GetAddress(o1); Marshal.Copy(ptr, data, 0, data.Length); } ); InteropTools.Pin( target, o2 => { var ptr = UnsafeTools.GetAddress(o2); Marshal.Copy(data, 0, ptr, data.Length); } ); }
public static unsafe int GetOffset(this FieldInfo field) { return(InteropTools.Pin( FormatterServices.GetUninitializedObject(field.DeclaringType), delegate(object inst) { TypedReference tr0, tr1; TypedReferenceTools.MakeTypedReference(&tr0, inst); TypedReferenceTools.MakeTypedReference(&tr1, inst, field); byte *p0 = (byte *)tr0.ToPointer(); byte *p1 = (byte *)tr1.ToPointer(); return (int)(p1 - p0); } )); }
/// <summary> /// Pins an object in a memory and constructs its pin handle. /// </summary> /// <param name="obj">The object to pin.</param> public ObjectPinHandle(object obj) { Object = obj; using (var re1 = new AutoResetEvent(false)) { var thr = new Thread( delegate() { InteropTools.Pin( obj, delegate { re1.Set(); Reset.WaitOne(); } ); } ); thr.Start(); re1.WaitOne(); } }