2012-07-16 4 views
4

두 개의 서로 다른 열거 형을 매핑 할 수 있습니까?ValueInjecter가있는 열거 형 사이의 맵

즉 하나의 열거 형 값을 가져와 다른 열거 형의 해당 값에 매핑하려고합니다.

나는 AutoMapper이 작업을 수행하는 방법을 알고 :

// Here's how to configure... 
Mapper.CreateMap<EnumSourceType, EnumTargetType>(); 

// ...and here's how to map 
Mapper.Map<EnumTargetType>(enumSourceValue) 

하지만 ValueInjecter에 새로 온 사람과 그것을 알아낼 수 없습니다.

** UPDATE **

소스 및 대상 열거 타입은 같은 모양 :

public enum EnumSourceType 
{ 
    Val1 = 0, 
    Val2 = 1, 
    Val3 = 2, 
    Val4 = 4, 
} 

public enum EnumTargetType 
{ 
    Val1, 
    Val2, 
    Val3, 
    Val4, 
} 

그래서, 상수가 같은 이름을 가질 수 있지만 다른 값을. 내가 문자열에서 캐스팅 Enum.Parse를 사용한 후 확인

+0

는 같은 크기의 열거입니까? 값을 e1 -> int -> e2로 바꾸고 싶습니까? – Omu

+0

둘 다'int'를 기본 유형으로 사용합니다. 문자열 이름은 두 열거 형 모두에서 동일하지만 정수 값이 다릅니다. – Cocowalla

답변

6

이 솔루션은, 나는

public class EnumsByStringName : ConventionInjection 
{ 
    protected override bool Match(ConventionInfo c) 
    { 
     return c.SourceProp.Name == c.TargetProp.Name 
      && c.SourceProp.Type.IsEnum 
      && c.TargetProp.Type.IsEnum; 
    } 

    protected override object SetValue(ConventionInfo c) 
    { 
     return Enum.Parse(c.TargetProp.Type, c.SourceProp.Value.ToString()); 
    } 
} 

public class F1 
{ 
    public EnumTargetType P1 { get; set; } 
} 

[Test] 
public void Tests() 
{ 
    var s = new { P1 = EnumSourceType.Val3 }; 
    var t = new F1(); 
    t.InjectFrom<EnumsByStringName>(s); 

    Assert.AreEqual(t.P1, EnumTargetType.Val3); 
} 
+0

클래스에서 enum을 래핑하는 것처럼 보입니다. 내 AutoMapper 예제 에서처럼 직접 매핑을 수행 할 수 있습니까? – Cocowalla

+0

EnumsByStringName은 valueinjection입니다. valueinjectioner가 작동하는 방식입니다. valueinjection을 생성하고 사용하는 몇 가지 맞춤 작업을 수행해야합니다. valueinjection은 일부 유형에만 국한되지 않고 모든 enum과 함께 작동합니다 – Omu

0
enum EnumSourceType 
    { 
     Val1 = 0, 
     Val2 = 1, 
     Val3 = 2, 
     Val4 = 4, 
    } 

    enum EnumTargetType 
    { 
     Targ1, 
     Targ2, 
     Targ3, 
     Targ4, 
    } 

    Dictionary<EnumSourceType, EnumTargetType> SourceToTargetMap = new Dictionary<EnumSourceType, EnumTargetType> 
    { 
     {EnumSourceType.Val1, EnumTargetType.Targ1}, 
     {EnumSourceType.Val2, EnumTargetType.Targ2}, 
     {EnumSourceType.Val3, EnumTargetType.Targ3}, 
     {EnumSourceType.Val4, EnumTargetType.Targ4}, 
    }; 

    Console.WriteLine(SourceToTargetMap[EnumSourceType.Val1]) 
을 열거하는 그들은 모두 열거 형을 것을 이름으로 속성과 사실과 일치하도록 규칙 주입을 매우 간단 사용한다 다음 버전이 LoopInjection 기본 클래스 사용하고
0

:

public class EnumsByStringName : LoopInjection 
    { 


     protected override bool MatchTypes(Type source, Type target) 
     { 
      return ((target.IsSubclassOf(typeof(Enum)) 
         || Nullable.GetUnderlyingType(target) != null && Nullable.GetUnderlyingType(target).IsEnum) 
        && (source.IsSubclassOf(typeof(Enum)) 
         || Nullable.GetUnderlyingType(source) != null && Nullable.GetUnderlyingType(source).IsEnum) 
        ); 
     } 

     protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp) 
     { 
      tp.SetValue(target, Enum.Parse(tp.PropertyType, sp.GetValue(source).ToString())); 
     } 
    }