/// <summary> /// 원본 인스턴스의 속성 값을 읽어와 대상 인스턴스의 속성에 매핑합니다. /// </summary> /// <typeparam name="TTarget">대상 인스턴스 수형</typeparam> /// <param name="source">원본 인스턴스</param> /// <param name="targetFactory">대상 인스턴스 생성 함수</param> /// <param name="mapOptions">매핑 시의 옵션</param> /// <param name="propertyExprsToExclude">제외할 속성명</param> /// <result>매핑 대상 인스턴스</result> public static TTarget MapProperty <TTarget>(this object source, Func <TTarget> targetFactory, MapPropertyOptions mapOptions, params Expression <Func <TTarget, object> >[] propertyExprsToExclude) { targetFactory.ShouldNotBeNull("targetFactory"); var target = targetFactory(); target.ShouldNotBeDefault("target"); if (IsDebugEnabled) { log.Debug("소스 인스턴스의 속성 정보를 대상 인스턴스[{2}]의 속성 값에 설정합니다. source=[{0}], target=[{1}]", source, target, typeof(TTarget).FullName); } var sourceAccessor = DynamicAccessorFactory.CreateDynamicAccessor(source.GetType(), mapOptions.SuppressException); var targetAccessor = DynamicAccessorFactory.CreateDynamicAccessor <TTarget>(mapOptions.SuppressException); var propertyNamesToExclude = propertyExprsToExclude.Select(expr => LinqTool.FindPropertyName(expr.Body)); var excludes = new List <string>(propertyNamesToExclude); if (IsDebugEnabled) { log.Debug("속성 설젱에서 제외할 속성들=[{0}]", excludes.CollectionToString()); } var sourcePropertyNames = sourceAccessor.GetPropertyNames(); var targetPropertyNames = targetAccessor.GetPropertyNames().Except(excludes).ToList(); if (IsDebugEnabled) { log.Debug("설정할 속성들=[{0}]", targetPropertyNames.CollectionToString()); } var sourceTypeName = source.GetType().FullName; var targetTypeName = typeof(TTarget).FullName; foreach (var propertyName in targetPropertyNames) { var targetName = propertyName; if (excludes.Any(epn => StringTool.EqualTo(epn, targetName))) { continue; } var sourceName = sourcePropertyNames.FirstOrDefault(spn => StringTool.EqualTo(spn, targetName)); if (sourceName.IsNotWhiteSpace()) { if (mapOptions.SkipAlreadyExistValue) { var targetType = targetAccessor.GetPropertyType(targetName); var targetValue = targetAccessor.GetPropertyValue(target, targetName); if (Equals(targetValue, targetType.GetTypeDefaultValue()) == false) { if (IsDebugEnabled) { log.Debug("대상 객체의 속성[{0}]에 이미 값이 설정되어 있어, 설정을 건너뜁니다. 속성값=[{1}]", targetName, targetValue); } continue; } } if (IsDebugEnabled) { log.Debug("원본객체[{0}] => 대상객체[{1}]로 속성[{2}]의 값을 할당합니다...", sourceTypeName, targetTypeName, propertyName); } var propertyValue = sourceAccessor.GetPropertyValue(source, sourceName); targetAccessor.SetPropertyValue(target, targetName, propertyValue); if (IsDebugEnabled) { log.Debug("속성[{0}]에 할당된 값은 [{1}]입니다.", targetName, propertyValue); } } } return(target); }