예제 #1
0
        static UnmanagedInstance()
        {
            var type = TypeOf <T> .TypeID;

            tptr  = type.TypeHandle.Value;
            tsize = UnsafeTools.BaseInstanceSizeOf(type);
            init  = new byte[tsize];
            BitConverter.GetBytes((long)tptr).CopyTo(init, 4);
        }
예제 #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);
        }