728x90
반응형
728x170
using System;
using System.Linq;
using System.Reflection;
#region 값 복사하기 - CopyValue(source, target, excludePropertyList, bindingFlags)
/// <summary>
/// 값 복사하기
/// </summary>
/// <param name="source">소스 객체</param>
/// <param name="target">타겟 객체</param>
/// <param name="excludePropertyList">제외 속성 리스트</param>
/// <param name="bindingFlags">바인딩 플래그</param>
/// <remarks>
/// BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase
/// </remarks>
public void CopyValue(object source, object target, string excludePropertyList, BindingFlags bindingFlags)
{
string[] excludePropertyArray = null;
if(!string.IsNullOrEmpty(excludePropertyList))
{
excludePropertyArray = excludePropertyList.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
MemberInfo[] memnerInfoArray = target.GetType().GetMembers(bindingFlags);
foreach(MemberInfo memnerInfo in memnerInfoArray)
{
string memberName = memnerInfo.Name;
if(!string.IsNullOrEmpty(excludePropertyList) && excludePropertyArray.Contains(memberName))
{
continue;
}
if(memnerInfo.MemberType == MemberTypes.Field)
{
FieldInfo sourceFieldInfo = source.GetType().GetField(memberName);
if(sourceFieldInfo == null)
{
continue;
}
object sourceValue = sourceFieldInfo.GetValue(source);
((FieldInfo)memnerInfo).SetValue(target, sourceValue);
}
else if(memnerInfo.MemberType == MemberTypes.Property)
{
PropertyInfo targetPropertyInfo = memnerInfo as PropertyInfo;
PropertyInfo sourcePropertyInfo = source.GetType().GetProperty(memberName, bindingFlags);
if(sourcePropertyInfo == null)
{
continue;
}
if(targetPropertyInfo.CanWrite && sourcePropertyInfo.CanRead)
{
object SourceValue = sourcePropertyInfo.GetValue(source, null);
targetPropertyInfo.SetValue(target, SourceValue, null);
}
}
}
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[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] IDataReader 인터페이스 : 제네릭 리스트 구하기 (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 |
[C#/COMMON] 임의(Random) ID 구하기 (0) | 2020.02.02 |
[C#/COMMON] 바이트 크기 단위 변환하기 (0) | 2020.02.02 |
댓글을 달아 주세요