简体   繁体   中英

Automapper mapping Enum to Class

I get the generic error of 'Missing type map configuration or unsupported mapping.' when I try to create this map. Any ideas?

     Mapper.CreateMap<MyEnum, MyClass>().ConvertUsing(c =>
     {
        MyAttribute attribute = c.GetCustomAttribute<MyEnum, MyAttribute>();
        return new MyClass()
        {
           Id = c.ToString(),
           Name = attribute == null ? c.ToString() : attribute.DisplayName
        };
     });

And...

  protected override void Configure()
  {
     base.Configure();

     Mapper.CreateMap<MyEnum, MyClass>()
        .ForMember(d => d.Id, opt => opt.MapFrom(s => s.ToString()))
        .ForMember(d => d.Name, opt => opt.ResolveUsing<DisplayNameResolver>());
  }

  private class DisplayNameResolver : ValueResolver<MyEnum, string>
  {
     protected override string ResolveCore(MyEnum e)
     {
        MyAttribute attribute = e.GetCustomAttribute<MyEnum, MyAttribute>();
        return attribute == null ? e.ToString() : attribute.DisplayName;
     }
  }

Don't seem to work.

Thanks.

One symptom of this error is that you did not call the Configure() in your application root. If there is a problem with your configuration.. I would suggest you make a Unit test and call Automapper's AssertConfigurationIsValid() - Configuration Validation Page

[TestMethod]
public void BaseMapperWorks()
{   
    //MapperConfig is my static MapperCongfiguration Class
    MapperConfig.Configure();
    Mapper.AssertConfigurationIsValid();
}

AutoMapper Validatior will give you everything that is wrong with your mappings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM