Ejemplo n.º 1
0
        public void WriteStruct <T>(T srcstruct) where T : struct
        {
            // Make sure the struct is in the correct byte-order
            srcstruct = BinaryByteOrder.ChangeStructOrder <T>(srcstruct, _byteOrder);

            byte[]   buffer = new byte[Marshal.SizeOf(srcstruct)];
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                Marshal.StructureToPtr(srcstruct, handle.AddrOfPinnedObject(), false);
                base.Write(buffer);
            }
            finally
            {
                handle.Free();
            }
        }
Ejemplo n.º 2
0
        public T ReadStruct <T>() where T : struct
        {
            T    result;
            Type structtype = typeof(T);

            byte[]   buffer = this.ReadBytes(Marshal.SizeOf(structtype));
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), structtype);

                result = BinaryByteOrder.ChangeStructOrder <T>(result, _byteOrder);
            }
            finally
            {
                handle.Free();
            }

            return(result);
        }