Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AutoMapper;
- using AutoMapper.Attributes;
- using System.Reflection;
- namespace ConsoleApp2
- {
- class MapsFromSourceAttribute : MapsFromAttribute
- {
- public MapsFromSourceAttribute() : base(typeof(Source)) { }
- public void ConfigureMapping(IMappingExpression<Source, Destination> mappingExpression)
- {
- mappingExpression.ForMember(T => T.BarValue, T => T.MapFrom(W => W.FooValue));
- }
- }
- class MapsFromSourceExAttribute : MapsFromAttribute
- {
- public MapsFromSourceExAttribute() : base(typeof(Source)) { }
- public void ConfigureMapping(IMappingExpression<Source, DestinationEx> mappingExpression)
- {
- // This >>GETS<< called, but the result is like it doesn't at all!
- mappingExpression.ForMember(T => T.BarValue, T => T.MapFrom(W => W.FooValue)); // This property is here again since this is independent mapping.
- mappingExpression.ForMember(T => T.FooBarValue, T => T.MapFrom(W => -W.FooValue)); // New property, but both don't get mapped.
- }
- }
- class Source
- {
- public string Property { get; set; }
- public int FooValue { get; set; }
- }
- [MapsFromSource] // If this attribute is removed, than the issue is RESOLVED. But we don't want to remove it, they're supposed to work independently.
- class Destination
- {
- public string Property { get; set; }
- public int BarValue { get; set; }
- }
- [MapsFromSourceEx]
- class DestinationEx : Destination
- {
- public int FooBarValue { get; set; }
- }
- class Program
- {
- static Program()
- {
- Mapper.Initialize(c =>
- {
- // Manual mapping here is overridden and the issue persists.
- Assembly.GetExecutingAssembly().MapTypes(c);
- // Manual mapping here SOLVES the issue, and the properties are mapped successfully.
- });
- }
- static void Main(string[] args)
- {
- Source s = new Source()
- {
- Property = "stringProp",
- FooValue = 1
- };
- // d's properties BarValue and FooBarValue are not mapped!
- DestinationEx d = Mapper.Map<DestinationEx>(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement