728x90
반응형
        /// <summary>
        /// Byte[] 를 Struct 로 변환합니다.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private T ConvertSrtuctFromBytes<T>(byte[] bytes) where T : struct
        {
            int size = Marshal.SizeOf(typeof(T));

            if (size > bytes.Length)
            {
                return default(T);
            }

            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(bytes, 0, ptr, size);
            T result = (T)Marshal.PtrToStructure(ptr, typeof(T));
            Marshal.FreeHGlobal(ptr);

            return result;
        }
반응형

+ Recent posts