첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ IDataReader 인터페이스 : 열거 가능형 구하기

------------------------------------------------------------------------------------------------------------------------

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

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요