첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

#region 열거 가능형 구하기 - GetEnumerable<T>(source, skipFieldList, propertyInfoDictionary)

/// <summary>
/// 열거 가능형 구하기
/// </summary>
/// <typeparam name="T">항목 타입</typeparam>
/// <param name="source">소스 데이터 리더</param>
/// <param name="skipFieldList">스킵 필드 리스트</param>
/// <param name="propertyInfoDictionary">속성 정보 딕셔너리</param>
/// <returns>열거 가능형</returns>
public IEnumerable<T> GetEnumerable<T>
(
    IDataReader                      source,
    string                           skipFieldList          = null,
    Dictionary<string, PropertyInfo> propertyInfoDictionary = null
) where T : new()
{
    if(source != null)
    {
        using(source)
        {
            if(propertyInfoDictionary == null)
            {
                propertyInfoDictionary = new Dictionary<string, PropertyInfo>();

                PropertyInfo[] propertyInfoArray = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);

                foreach(PropertyInfo propertyInfo in propertyInfoArray)
                {
                    propertyInfoDictionary.Add(propertyInfo.Name.ToLower(), propertyInfo);
                }
            }

            while(source.Read())
            {
                T instance = new T();

                CopyValue(source, instance, skipFieldList, propertyInfoDictionary);

                yield return instance;
            }
        }
    }
}

#endregion
#region 값 복사하기 - CopyValue(source, target, skipFieldList, propertyInfoDictionary)

/// <summary>
/// 값 복사하기
/// </summary>
/// <param name="source">소스 데이터 리더</param>
/// <param name="target">타겟 객체</param>
/// <param name="skipFieldList">스킬 필드 리스트</param>
/// <param name="propertyInfoDictionary">속성 정보 딕셔너리</param>
private void CopyValue
(
    IDataReader                      source,
    object                           target,
    string                           skipFieldList          = null,
    Dictionary<string, PropertyInfo> propertyInfoDictionary = null
)
{
    if(source.IsClosed)
    {
        throw new InvalidOperationException("Source data reader is closed.");
    }

    if(string.IsNullOrWhiteSpace(skipFieldList))
    {
        skipFieldList = string.Empty;
    }
    else
    {
        skipFieldList = "," + skipFieldList + ",";
    }

    skipFieldList = skipFieldList.ToLower();

    if(propertyInfoDictionary == null || propertyInfoDictionary.Count < 1)
    {
        if(propertyInfoDictionary == null)
        {
            propertyInfoDictionary = new Dictionary<string, PropertyInfo>();
        }

        PropertyInfo[] propertyInfoArray = target.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

        foreach(PropertyInfo propertyInfo in propertyInfoArray)
        {
            propertyInfoDictionary.Add(propertyInfo.Name.ToLower(), propertyInfo);
        }
    }

    for(int i = 0; i < source.FieldCount; i++)
    {
        string fieldName = source.GetName(i).ToLower();

        if(propertyInfoDictionary.ContainsKey(fieldName))
        {
            PropertyInfo propertyInfo = propertyInfoDictionary[fieldName];

            if(!string.IsNullOrEmpty(skipFieldList) && skipFieldList.Contains("," + fieldName + ","))
            {
                continue;
            }

            if((propertyInfo != null) && propertyInfo.CanWrite)
            {
                object value = source.GetValue(i);

                if(value == DBNull.Value)
                {
                    value = null;
                }
                else if(propertyInfo.PropertyType == typeof(bool) && value is long)
                {
                    value = (long)value == 1 ? true : false;
                }

                propertyInfo.SetValue(target, value, null);
            }
        }
    }
}
#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요