public static unsafe void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld) { if (structure == null) { throw new ArgumentNullException(nameof(structure)); } if (ptr == IntPtr.Zero) { throw new ArgumentNullException(nameof(ptr)); } if (fDeleteOld) { DestroyStructure(ptr, structure.GetType()); } RuntimeTypeHandle structureTypeHandle = structure.GetType().TypeHandle; if (structureTypeHandle.IsGenericType() || structureTypeHandle.IsGenericTypeDefinition()) { throw new ArgumentException(SR.Argument_NeedNonGenericObject, nameof(structure)); } IntPtr marshalStub; if (structureTypeHandle.IsBlittable()) { if (!RuntimeInteropData.TryGetStructMarshalStub(structureTypeHandle, out marshalStub)) { marshalStub = IntPtr.Zero; } } else { marshalStub = RuntimeInteropData.GetStructMarshalStub(structureTypeHandle); } if (marshalStub != IntPtr.Zero) { if (structureTypeHandle.IsValueType()) { ((delegate * < ref byte, ref byte, void >)marshalStub)(ref structure.GetRawData(), ref *(byte *)ptr); } else { ((delegate * < object, ref byte, void >)marshalStub)(structure, ref *(byte *)ptr); } } else { nuint size = (nuint)RuntimeInteropData.GetStructUnsafeStructSize(structureTypeHandle); Buffer.Memmove(ref *(byte *)ptr, ref structure.GetRawData(), size); } }
internal static unsafe void PtrToStructureImpl(IntPtr ptr, object structure) { RuntimeTypeHandle structureTypeHandle = structure.GetType().TypeHandle; IntPtr unmarshalStub; if (structureTypeHandle.IsBlittable()) { if (!RuntimeInteropData.TryGetStructUnmarshalStub(structureTypeHandle, out unmarshalStub)) { unmarshalStub = IntPtr.Zero; } } else { unmarshalStub = RuntimeInteropData.GetStructUnmarshalStub(structureTypeHandle); } if (unmarshalStub != IntPtr.Zero) { if (structureTypeHandle.IsValueType()) { ((delegate * < ref byte, ref byte, void >)unmarshalStub)(ref *(byte *)ptr, ref structure.GetRawData()); } else { ((delegate * < ref byte, object, void >)unmarshalStub)(ref *(byte *)ptr, structure); } } else { nuint size = (nuint)RuntimeInteropData.GetStructUnsafeStructSize(structureTypeHandle); Buffer.Memmove(ref structure.GetRawData(), ref *(byte *)ptr, size); } }
internal static int SizeOfHelper(Type t, bool throwIfNotMarshalable) { Debug.Assert(throwIfNotMarshalable); return(RuntimeInteropData.GetStructUnsafeStructSize(t.TypeHandle)); }