【问题标题】:Map between enums with ValueInjecter使用 ValueInjecter 在枚举之间映射
【发布时间】:2012-07-16 12:06:09
【问题描述】:

是否可以在 2 个不同的枚举之间进行映射?

也就是说,我想取一个枚举值并将其映射到不同枚举类型中的对应值。

我知道如何使用 AutoMapper:

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

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

但我是 ValueInjecter 的新手,无法弄清楚。

** 更新 **

源枚举类型和目标枚举类型如下所示:

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

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

因此,常量名称相同,但值不同。

【问题讨论】:

  • 枚举的大小是否相同?你希望这些值只是 e1 -> int -> e2 吗?
  • 它们都使用int 作为底层类型。两种枚举类型的字符串名称相同,但整数值不同。

标签: c# enums valueinjecter


【解决方案1】:

好的,解决方案很简单,我使用约定注入按名称匹配属性,并且在我使用 Enum.Parse 从字符串转换为枚举之后它们都是枚举的事实

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);
}

【讨论】:

  • 看起来您正在将枚举包装在一个类中 - 是否可以像我的 AutoMapper 示例中那样直接进行映射?
  • EnumsByStringName 是一个 valueinjection,这就是 valueinjecter 的工作方式,当你需要做一些自定义的东西时,你创建一个 valueinjection 并使用它;您可能会注意到 valueinjection 并不特定于某些类型,它适用于任何枚举
【解决方案2】:
    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] )

【讨论】:

    【解决方案3】:

    这是一个使用 LoopInjection 基类的版本:

    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()));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2016-03-23
      相关资源
      最近更新 更多