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

TestProject.zip
다운로드

▶ Program.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Console.WriteLine(TypeHelper.IsSameType(20, typeof(int)));

            Console.WriteLine(TypeHelper.IsPrimitiveType(typeof(int?)));

            Console.WriteLine(TypeHelper.IsNumericType(typeof(int?)));

            Console.WriteLine(TypeHelper.IsNullableType(typeof(int?)));
        }

        #endregion
    }
}

 

▶ TypeHelper.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace TestProject
{
    /// <summary>
    /// 타입 헬퍼
    /// </summary>
    public static class TypeHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 문자열 타입
        /// </summary>
        public static Type StringType = typeof(string);

        /// <summary>
        /// Type 타입
        /// </summary>
        public static Type TypeType = typeof(Type);

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 기본 타입 세트
        /// </summary>
        private static HashSet<Type> _primitiveTypeSet;

        /// <summary>
        /// 숫자 타입 세트
        /// </summary>
        private static HashSet<Type> _numericTypeSet;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static

        #region 생성자 - TypeHelper()

        /// <summary>
        /// 생성자
        /// </summary>
        static TypeHelper()
        {
            _primitiveTypeSet = new HashSet<Type>()
            {
                typeof(bool),
                typeof(string),
                typeof(DateTime),
                typeof(Type)
            };

            _numericTypeSet = new HashSet<Type>
            {
                typeof(byte),
                typeof(sbyte),
                typeof(int),
                typeof(uint),
                typeof(short),
                typeof(ushort),
                typeof(long),
                typeof(ulong),
                typeof(float),
                typeof(double),
                typeof(decimal)
            };

            #region NULLABLE 기본 타입을 추가한다.

            List<Type> nullablePrimitiveTypeList = new List<Type>();

            foreach(Type primitiveType in _primitiveTypeSet)
            {
                Type nullablePrimitiveType = GetNullableType(primitiveType);

                if(nullablePrimitiveType != primitiveType)
                {
                    nullablePrimitiveTypeList.Add(nullablePrimitiveType);
                }
            }

            foreach(Type nullablePrimitiveType in nullablePrimitiveTypeList)
            {
                _primitiveTypeSet.Add(nullablePrimitiveType);
            }

            #endregion
            #region NULLABLE 숫자 타입을 추가한다.

            List<Type> nullableNumericTypeList = new List<Type>();

            foreach(Type numericType in _numericTypeSet)
            {
                Type nullableNumericType = GetNullableType(numericType);

                if(nullableNumericType != numericType)
                {
                    nullableNumericTypeList.Add(nullableNumericType);
                }
            }

            foreach(Type nullableNumericType in nullableNumericTypeList)
            {
                _numericTypeSet.Add(nullableNumericType);
            }

            #endregion

            foreach(Type numericType in _numericTypeSet)
            {
                _primitiveTypeSet.Add(numericType);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 동일 타입 여부 구하기 - IsSameType(value, type)

        /// <summary>
        /// 동일 타입 여부 구하기
        /// </summary>
        /// <param name="value">값</param>
        /// <param name="type">타입</param>
        /// <returns>동일 타입 여부</returns>
        public static bool IsSameType(object value, Type type)
        {
            if((value is Type && type == TypeType) || value.GetType() == type)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
        #region 기본 타입 여부 구하기 - IsPrimitiveType(type)

        /// <summary>
        /// 기본 타입 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>기본 타입 여부</returns>
        public static bool IsPrimitiveType(Type type)
        {
            return _primitiveTypeSet.Contains(type);
        }

        #endregion
        #region 숫자 타입 여부 구하기 - IsNumericType(type)

        /// <summary>
        /// 숫자 타입 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>숫자 타입 여부</returns>
        public static bool IsNumericType(Type type)
        {
            return _numericTypeSet.Contains(type);
        }

        #endregion
        #region NULLABLE 타입 여부 구하기 - IsNullableType(type)

        /// <summary>
        /// NULLABLE 타입 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>NULLABLE 타입 여부</returns>
        public static bool IsNullableType(Type type)
        {
            if(type == null)
            {
                return false;
            }

            if(type.IsValueType == false)
            {
                return true;
            }

            return type.IsGenericType == true && type.GetGenericTypeDefinition() == typeof(Nullable<>);
        }

        #endregion
        #region NULLABLE 타입 구하기 - GetNullableType(type)


        /// <summary>
        /// NULLABLE 타입 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>NULLABLE 타입</returns>
        public static Type GetNullableType(Type type)
        {
            if(type == null)
            {
                return null;
            }

            if(IsNullableType(type) == false)
            {
                return typeof(Nullable<>).MakeGenericType(type);
            }
            else
            {
                return type;
            }
        }

        #endregion
        #region NULLABLE 내부 타입 구하기 - GetNullableUnderlyingType(type)

        /// <summary>
        /// NULLABLE 내부 타입 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>NULLABLE 내부 타입</returns>
        public static Type GetNullableUnderlyingType(Type type)
        {
            if
            (
                type                 == null           ||
                type                 == typeof(object) ||
                type.BaseType        == typeof(object) ||
                type                 == typeof(Type)   ||
                IsNullableType(type) == false
            )
            {
                return type;
            }

            return Nullable.GetUnderlyingType(type);
        }

        #endregion
        #region 디폴트 값 구하기 - GetDefaultValue(type)

        /// <summary>
        /// 디폴트 값 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>디폴트 값</returns>
        public static object GetDefaultValue(Type type)
        {
            if(type?.IsValueType == true)
            {
                return Activator.CreateInstance(type);
            }
            else
            {
                return null;
            }
        }

        #endregion
        #region 리스트 항목 타입 구하기 - GetListItemType(type, listType, itemType)

        /// <summary>
        /// 리스트 항목 타입 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <param name="listType">리스트 타입</param>
        /// <param name="itemType">항목 타입</param>
        public static void GetListItemType(Type type, ref Type listType, ref Type itemType)
        {
            listType = null;
            itemType = null;

            if(IsListType(type) == true)
            {
                listType = type;
                itemType = type.GetGenericArguments().FirstOrDefault();
            }
            else
            {
                listType = null;
                itemType = type;
            }
        }

        #endregion
        #region 리스트 타입 여부 구하기 - IsListType(type)

        /// <summary>
        /// 리스트 타입 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>리스트 타입 여부</returns>
        public static bool IsListType(Type type)
        {
            if(type == null)
            {
                return false;
            }

            if
            (
                typeof(IList).IsAssignableFrom(type) == true ||
                type.GetInterfaces().Any
                (
                    i => i.IsGenericType == true && i.GetGenericTypeDefinition() == typeof(ICollection<>)
                )
            )
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
        #region 데이터 테이블 여부 구하기 - IsDataTableType(type)

        /// <summary>
        /// 데이터 테이블 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>데이터 테이블 여부</returns>
        public static bool IsDataTableType(Type type)
        {
            if(type == typeof(DataTable))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
        #region 데이터 리더 타입 여부 구하기 - IsDataReaderType(type)

        /// <summary>
        /// 데이터 리더 타입 여부 구하기
        /// </summary>
        /// <param name="type">타입</param>
        /// <returns>데이터 리더 타입 여부</returns>
        public static bool IsDataReaderType(Type type)
        {
            if(typeof(IDataReader).IsAssignableFrom(type) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
        #region 타입 배열 구하기 - GetTypeArray(assembly, namespaceName)

        /// <summary>
        /// 타입 배열 구하기
        /// </summary>
        /// <param name="assembly">어셈블리</param>
        /// <param name="namespaceName">네임스페이스명</param>
        /// <returns>타입 배열</returns>
        public static Type[] GetTypeArray(Assembly assembly, string namespaceName)
        {
            return assembly.GetTypes().Where
            (
                t => string.Equals(t.Namespace, namespaceName, StringComparison.Ordinal)
            ).ToArray();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요