첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요