2012-09-06 2 views
2

속성이 많은 다소 큰 개체가 있습니다.오토캡을 사용하여 사용하지 않는 속성을 무시합니다.

Automapper를 사용하여 표에서 속성에 매핑합니다.

는 속성 만 몇 매핑 할 필요가 있고 나머지는 그렇지 않은 매핑시 이후 사용으로

이 모든 속성을 '무시'또는 할 수있는 방법이 있나요 무시해야 모든 속성에 대해 명시 적으로 '무시'를 작성해야합니다 (아래 코드 참조). 하나씩 무시하는 대신 '.IgnoreAllNotUsed'할 수있게하고 싶습니다. 이것이 가능한가?

클래스는 다른 클래스에서 상속하지만 대부분의 속성은 당신이 확장 방법을 사용할 수있는 실제 클래스 자체 link to picture of code enter image description here

enter image description here

답변

3

에 있습니다

public static void ForAllUnmappedMembers<TSource, TDestination>(
    this IMappingExpression<TSource, TDestination> mapping, 
    Action<IMemberConfigurationExpression<TSource>> memberOptions) 
{ 
    var typeMap = Mapper.FindTypeMapFor<TSource, TDestination>(); 
    foreach(var memberName in typeMap.GetUnmappedPropertyNames()) 
     mapping.ForMember(memberName, memberOptions); 
} 

그것을 사용을 이렇게 :

Mapper.CreateMap<Source, Destination>() 
     .ForMember(...) 
     .ForAllUnmappedMembers(o => o.Ignore()); 

테스트하지는 않았지만 제대로 작동합니다.

6

모든 속성을 무시한 다음 ForMember를 지정하면됩니다. 여기 예가 있습니다 :

var mapping = Mapper.CreateMap<Source, Destination>(); 
mapping.ForAllMembers(opt=>opt.Ignore()); 
mapping.ForMember(...) 
     .ForMember(...); 
+0

나는 대답으로 1보다 많은 것을 표시 할 수 있습니다. 어쨌든 고마워. – kurasa

관련 문제