■ 바이트 배열에서 구조체 객체 구하기
------------------------------------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
#region 객체 구하기 - GetObject<T>(sourceByteArray)
/// <summary>
/// 객체 구하기
/// </summary>
/// <typeparam name="T">객체 타입</typeparam>
/// <param name="sourceByteArray">소스 바이트 배열</param>
/// <returns>객체</returns>
public static T GetObject<T>(byte[] sourceByteArray) where T : struct
{
int objectSize = Marshal.SizeOf(typeof(T));
if(objectSize > sourceByteArray.Length)
{
throw new Exception();
}
IntPtr handle = Marshal.AllocHGlobal(objectSize);
Marshal.Copy(sourceByteArray, 0, handle, objectSize);
T targetObject = (T)Marshal.PtrToStructure(handle, typeof(T));
Marshal.FreeHGlobal(handle);
return targetObject;
}
#endregion
------------------------------------------------------------------------------------------------------------------------
'C# > Common' 카테고리의 다른 글
[C#/COMMON] WIN32 API를 사용해 최상위 윈도우 여부 구하기 (0) | 2019.06.06 |
---|---|
[C#/COMMON] Process 클래스 : 윈도우즈 서비스 실행하기 (0) | 2019.06.06 |
[C#/COMMON] 공용체(Union) 구조체 사용하기 (0) | 2019.06.02 |
[C#/COMMON] Socket 클래스 : 소켓 연결시 시간 제한하기 (0) | 2019.06.02 |
[C#/COMMON] 파일 확장자 연결하기 (0) | 2019.05.30 |
[C#/COMMON] 바이트 배열에서 구조체 객체 구하기 (0) | 2019.05.27 |
[C#/COMMON] 구조체 바이트 배열 구하기 (0) | 2019.05.27 |
[C#/COMMON] Assembly 클래스 : 어셈블리 GUID 구하기 (0) | 2019.05.26 |
[C#/COMMON] Socket 클래스 : 호스트 IP 주소 구하기 (0) | 2019.05.26 |
[C#/COMMON] 특정 프로세스의 윈도우 상태 구하기 (0) | 2019.05.26 |
[C#/COMMON] 16진수 문자열 구하기 (0) | 2019.05.22 |
댓글을 달아 주세요