728x90
반응형
728x170
▶ StructureHelper.cs
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
namespace TestProject
{
/// <summary>
/// 구조체 헬퍼
/// </summary>
public static class StructureHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 타입 딕셔너리
/// </summary>
private static Dictionary<Type, int> _typeDictionary = new Dictionary<Type, int>();
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 크기 구하기 - SizeOf(type)
/// <summary>
/// 크기 구하기
/// </summary>
/// <param name="type">타입</param>
/// <returns>크기</returns>
public static int SizeOf(Type type)
{
int size;
if(_typeDictionary.TryGetValue(type, out size))
{
return size;
}
size = GetSize(type);
_typeDictionary.Add(type, size);
return size;
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 크기 - GetSize(type)
/// <summary>
/// 크기 구하기
/// </summary>
/// <param name="type">타입</param>
/// <returns>크기</returns>
private static int GetSize(Type type)
{
DynamicMethod dynamicMethod = new DynamicMethod("SizeOfType", typeof(int), new Type[] { });
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
ilGenerator.Emit(OpCodes.Sizeof, type);
ilGenerator.Emit(OpCodes.Ret);
return (int)dynamicMethod.Invoke(null, null);
}
#endregion
}
}
728x90
▶ Student.cs
using System;
using System.Runtime.InteropServices;
namespace TestProject
{
/// <summary>
/// 학생
/// </summary>
public unsafe struct Student
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// ID
/// </summary>
public int ID;
/// <summary>
/// 성명
/// </summary>
public fixed char Name[20];
/// <summary>
/// 생성 시간
/// </summary>
public DateTime CreateTime;
#endregion
}
}
300x250
▶ Program.cs
using System;
namespace TestProject
{
/// <summary>
/// 프로그램
/// </summary>
class Program
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
private static void Main()
{
int size = StructureHelper.SizeOf(typeof(Student));
Console.WriteLine($"Student 구조체 크기 : {size}");
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] MailAddress 클래스 : E 메일 주소 여부 구하기 (0) | 2022.06.20 |
---|---|
[C#/COMMON/.NET6] Buffer 클래스 : BlockCopy 정적 메소드를 사용해 인코딩 배제 문자열 바이트 배열 구하기 (0) | 2022.06.20 |
[C#/COMMON] MemoryMappedFile 클래스 : CreateFromFile 정적 메소드를 사용해 메모리 매핑 파일 만들기 (0) | 2022.05.25 |
[C#/COMMON] FileSystem 클래스 : CopyDirectory 정적 메소드를 사용해 디렉토리 복사하기 (0) | 2022.05.25 |
[C#/COMMON] Marshal 클래스 : Copy 정적 메소드를 사용해 문자열을 fixed char 필드에 복사하기 (0) | 2022.05.25 |
[C#/COMMON] MemoryMappedFile 클래스 : CreateNew 정적 메소드를 사용해 시스템 메모리에서 메모리 매핑 파일 만들기 (0) | 2022.05.25 |
[C#/COMMON/.NET5] 콘솔 폰트 설정하기 (0) | 2022.05.24 |
[C#/COMMON] WebClient 클라이언트 : 서버 인코딩 타입에 맞춰 문자열 다운로드하기 (0) | 2022.05.24 |
[C#/COMMON] 이스케이프 유니코드 문자열 구하기 (0) | 2022.05.23 |
[C#/COMMON] Process 클래스 : 원격 데스크톱 실행하기 (0) | 2022.05.13 |
댓글을 달아 주세요