Exemplo n.º 1
0
        /// <summary>
        /// Allocates unmanaged memory for an object.
        /// </summary>
        public static T Allocate()
        {
            IntPtr handle = Marshal.AllocHGlobal(tsize);

            Marshal.Copy(init, 0, handle, tsize);
            IntPtr ptr = handle + 4;

            return(UnsafeTools.GetObject(ptr) as T);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new object handle in the unmanaged memory.
        /// </summary>
        /// <param name="t">The type of the object.</param>
        public ObjectHandle(Type t)
        {
            tptr = t.TypeHandle.Value;
            int size = UnsafeTools.BaseInstanceSizeOf(t);

            handle = Marshal.AllocHGlobal(size);
            byte[] zero = new byte[size];
            Marshal.Copy(zero, 0, handle, size);
            IntPtr ptr = handle + 4;

            Marshal.WriteIntPtr(ptr, tptr);            //write type ptr
            value = (T)UnsafeTools.GetObject(ptr);
        }