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
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 인스턴스 생성하기 (0) | 2020.02.02 |
---|---|
[C#/COMMON] 인스턴스 생성하기 (0) | 2020.02.02 |
[C#/COMMON] Type 클래스 : 타입명으로 타입 구하기 (0) | 2020.02.02 |
[C#/COMMON] 메소드 실행하기 (0) | 2020.02.02 |
[C#/COMMON] Stream 클래스 : 스트림 복사하기 (0) | 2020.02.02 |
[C#/COMMON] IDataReader 인터페이스 : 제네릭 리스트 구하기 (0) | 2020.02.02 |
[C#/COMMON] 객체 값 복사하기 (0) | 2020.02.02 |
[C#/COMMON] DataRow 클래스 : 객체에서 값 복사하기 (0) | 2020.02.02 |
[C#/COMMON] DataRow 클래스 : 객체에 값 복사하기 (0) | 2020.02.02 |
[C#/COMMON] 숫자 임의(Random) ID 구하기 (0) | 2020.02.02 |
댓글을 달아 주세요