Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"No parameterless constructor defined for this object" when map inherited class #146

Open
everfrown opened this issue Dec 10, 2017 · 0 comments

Comments

@everfrown
Copy link

Hi,

I've found this strange behaviour while mapping inherited classes with nested field.

If we define two simple destination:

    class DestBase
    {
        public DestBase() { }
        public string Object { get; set; }
    }

    class Dest : DestBase
    {
        public Dest() { }
    }

and source class with nested object field

    class Source
    {
        public SomeObject Object { get; set; }
        public string ObjectStr { get; set; }
    }
    class SomeObject
    {
        public SomeObject() {}
    }

then try to map and compile them

    Mapper.Register<Source, DestBase>()
        .Member(dest => dest.Object, src => src.ObjectStr)
        .Include<Source, Dest>();
    Mapper.Register<Source, Dest>();

    Mapper.Compile();

we will catch exception No parameterless constructor defined for this object.

I think this caused by property Object name duplication while including rules:

  • If we change string property name DestBase.Object to some other (not as object property Source.Object), all works fine.
  • If we remove dest classes inheritance, all works fine.
  • And also if we duplicate registration in parent and child class, all works fine.
    Mapper.Register<Source, DestBase>()
        .Member(dest => dest.Object, src => src.ObjectStr)
        .Include<Source, Dest>();
    Mapper.Register<Source, Dest>()
        .Member(dest => dest.Object, src => src.ObjectStr);

    Mapper.Compile();

Is it normal? Should I duplicate member registration for all children?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant