2012-02-17 2 views

답변

0

는 여기에 내가이 작업을 수행하는 데 사용하는 확장 메서드입니다.

다음은 확장 프로그램 사용 방법의 예입니다.

var skipProperties = new[] { "Id", "DataSession_Id", "CoverNumber", "CusCode", "BoundAttempted", "BoundSuccess", "DataSession", "DataSessions","Carriers" }; 
DataSession.Quote = new Quote().CloneExcept(lastSession.Quote, skipProperties); 

확장 메서드로 구현되므로 호출하는 개체를 수정하고 편의상 반환합니다. 이것은 [질문]에서 논의되었습니다 : Best way to clone properties of disparate objects

0

자바에 대해 이야기하는 경우 "일시적인"키워드를 사용해보십시오. atleast이 serialization을 위해 작동합니다.

public static T CloneExcept<T, S>(this T target, S source, string[] propertyNames) 
{ 
    if (source == null) 
    { 
     return target; 
    } 
    Type sourceType = typeof(S); 
    Type targetType = typeof(T); 
    BindingFlags flags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance; 

    PropertyInfo[] properties = sourceType.GetProperties(); 
    foreach (PropertyInfo sPI in properties) 
    { 
     if (!propertyNames.Contains(sPI.Name)) 
     { 
      PropertyInfo tPI = targetType.GetProperty(sPI.Name, flags); 
      if (tPI != null && tPI.PropertyType.IsAssignableFrom(sPI.PropertyType)) 
      { 
       tPI.SetValue(target, sPI.GetValue(source, null), null); 
      } 
     } 
    } 
    return target; 
} 

또한 Automapper을 확인 수 있습니다

+1

아니요, 저는 C#을 사용하고 있습니다. – user282807

+0

@ user282807 다음 번에 특정 언어에 맞는 언어 태그를 설정하십시오. 관련없는 답변을 파싱하고 다른 언어를 작성하지 않아도됩니다 .- 고맙게도 멋진 커뮤니티 회원이 이미이 태그를 추가했습니다. – mbx

관련 문제