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

#region 값 복사하기 - Copy(source, target)

/// <summary>
/// 값 복사하기
/// </summary>
/// <param name="source">소스 객체</param>
/// <param name="target">타겟 데이터 행</param>
/// <returns>처리 결과</returns>
public bool CopyValue(object source, DataRow target)
{
    bool result = true;

    MemberInfo[] memberInfoArray = target.GetType().FindMembers
    (
        MemberTypes.Field | MemberTypes.Property,
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase,
        null,
        null
    );

    foreach(MemberInfo memberInfo in memberInfoArray)
    {
        string memberName = memberInfo.Name;

        if(!target.Table.Columns.Contains(memberName))
        {
            continue;
        }

        try
        {
            if(memberInfo.MemberType == MemberTypes.Field)
            {
                target[memberName] = ((FieldInfo)memberInfo).GetValue(target);
            }
            else if(memberInfo.MemberType == MemberTypes.Property)
            {
                target[memberName] = ((PropertyInfo)memberInfo).GetValue(target, null);
            }
        }
        catch
        {
            result = false;
        }
    }

    return result;
}

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

댓글을 달아 주세요