AutoMapper 字符串到枚举描述

发布于 2024-09-14 03:09:39 字数 1033 浏览 5 评论 0原文

给定要求:

获取一个对象图,根据第二个字符串属性的已处理值设置所有枚举类型属性。约定规定源字符串属性的名称将是带有后缀“Raw”的枚举属性的名称。

通过处理,我们的意思是我们需要删除指定的字符等,

我查看了自定义格式化程序、值解析器和类型转换器,其中没有一个似乎是解决此问题的方法?

我们希望使用 AutoMapper 而不是我们自己的反射例程,原因有两个,a)它在项目的其余部分中广泛使用,b)它为您提供递归遍历 ootb。

-- 示例 --

给定下面的(简单)结构,以及:

var tmp = new SimpleClass 
  { 
       CountryRaw = "United States",
       Person = new Person { GenderRaw="Male" }
  };

var tmp2 = new SimpleClass();

Mapper.Map(tmp, tmp2);

我们期望 tmp2 的 MappedCountry 枚举为 Country.UnitedStates,并且 Person 属性的性别为 Gender.Male。

public class SimpleClass1
{
  public string CountryRaw {get;set;}

  public Country MappedCountry {get;set;}

  public Person Person {get;set;}
}

public class Person
{
  public string GenderRaw {get;set;}

  public Gender Gender {get;set;}

  public string Surname {get;set;}
}

public enum Country
{
  UnitedStates = 1,
  NewZealand = 2
}

public enum Gender
{
  Male,
  Female,
  Unknown
}

谢谢

Given the requirement:

Take an object graph, set all enum type properties based on the processed value of a second string property. Convention dictates that the name of the source string property will be that of the enum property with a postfix of "Raw".

By processed we mean we'll need to strip specified characters e.t.c.

I've looked at custom formatters, value resolvers and type converters, none of which seems like a solution for this?

We want to use AutoMapper as opposed to our own reflection routine for two reasons, a) it's used extensively throughout the rest of the project and b) it gives you recursive traversal ootb.

-- Example --

Given the (simple) structure below, and this:

var tmp = new SimpleClass 
  { 
       CountryRaw = "United States",
       Person = new Person { GenderRaw="Male" }
  };

var tmp2 = new SimpleClass();

Mapper.Map(tmp, tmp2);

we'd expect tmp2's MappedCountry enum to be Country.UnitedStates and the Person property to have a gender of Gender.Male.

public class SimpleClass1
{
  public string CountryRaw {get;set;}

  public Country MappedCountry {get;set;}

  public Person Person {get;set;}
}

public class Person
{
  public string GenderRaw {get;set;}

  public Gender Gender {get;set;}

  public string Surname {get;set;}
}

public enum Country
{
  UnitedStates = 1,
  NewZealand = 2
}

public enum Gender
{
  Male,
  Female,
  Unknown
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-09-21 03:09:39

我用 ValueInjecter 做到了,
整个事情是这样的:

我在 SimpleClass 中又添加了一个道具,只是为了向您展示它是如何工作的

public class SixFootUnderTest
{
    [Test]
    public void Test()
    {
        var o = new SimpleClass1
                    {
                        CountryRaw = "United States",
                        GenderRaw = "Female",
                        Person = new Person { GenderRaw = "Male" }
                    };

        var oo = new SimpleClass1();

        oo.InjectFrom(o)
            .InjectFrom<StrRawToEnum>(o);
        oo.Person.InjectFrom<StrRawToEnum>(o.Person);

        oo.Country.IsEqualTo(Country.UnitedStates);
        oo.Gender.IsEqualTo(Gender.Female);
        oo.Person.Gender.IsEqualTo(Gender.Male);
    }

    public class SimpleClass1
    {
        public string CountryRaw { get; set; }

        public Country Country { get; set; }

        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public Person Person { get; set; }
    }

    public class Person
    {
        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public string Surname { get; set; }
    }


    public class StrRawToEnum : LoopValueInjection
    {
        protected override bool UseSourceProp(string sourcePropName)
        {
            return sourcePropName.EndsWith("Raw");
        }

        protected override string TargetPropName(string sourcePropName)
        {
            return sourcePropName.RemoveSuffix("Raw");
        }

        protected override bool TypesMatch(Type sourceType, Type targetType)
        {
            return sourceType == typeof(string) && targetType.IsEnum;
        }

        protected override object SetValue(object sourcePropertyValue)
        {
            return Enum.Parse(TargetPropType, sourcePropertyValue.ToString().Replace(" ", ""), true);
        }
    }

    public enum Country
    {
        UnitedStates = 1,
        NewZealand = 2
    }


    public enum Gender
    {
        Male,
        Female,
        Unknown
    }
}

,以防万一您需要从 CountryRaw 到 MappedCountry 进行操作
你可以这样做:

oo.InjectFrom(new StrRawToEnum().TargetPrefix("Mapped"), o);

I did it with the ValueInjecter,
here is the whole thing:

I've added one more prop to the SimpleClass just to show you how it works

public class SixFootUnderTest
{
    [Test]
    public void Test()
    {
        var o = new SimpleClass1
                    {
                        CountryRaw = "United States",
                        GenderRaw = "Female",
                        Person = new Person { GenderRaw = "Male" }
                    };

        var oo = new SimpleClass1();

        oo.InjectFrom(o)
            .InjectFrom<StrRawToEnum>(o);
        oo.Person.InjectFrom<StrRawToEnum>(o.Person);

        oo.Country.IsEqualTo(Country.UnitedStates);
        oo.Gender.IsEqualTo(Gender.Female);
        oo.Person.Gender.IsEqualTo(Gender.Male);
    }

    public class SimpleClass1
    {
        public string CountryRaw { get; set; }

        public Country Country { get; set; }

        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public Person Person { get; set; }
    }

    public class Person
    {
        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public string Surname { get; set; }
    }


    public class StrRawToEnum : LoopValueInjection
    {
        protected override bool UseSourceProp(string sourcePropName)
        {
            return sourcePropName.EndsWith("Raw");
        }

        protected override string TargetPropName(string sourcePropName)
        {
            return sourcePropName.RemoveSuffix("Raw");
        }

        protected override bool TypesMatch(Type sourceType, Type targetType)
        {
            return sourceType == typeof(string) && targetType.IsEnum;
        }

        protected override object SetValue(object sourcePropertyValue)
        {
            return Enum.Parse(TargetPropType, sourcePropertyValue.ToString().Replace(" ", ""), true);
        }
    }

    public enum Country
    {
        UnitedStates = 1,
        NewZealand = 2
    }


    public enum Gender
    {
        Male,
        Female,
        Unknown
    }
}

also in case you need to do it from CountryRaw to MappedCountry
you could do it like this:

oo.InjectFrom(new StrRawToEnum().TargetPrefix("Mapped"), o);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文