Mapping derived classes not working
See original GitHub issueHi,
I have the following classes:
public class TestClass
{
public List<BaseClass> MyProperty { get; set; }
}
public class BaseClass {}
public class ClassA: BaseClass {}
public class ClassB: BaseClass {}
If I run the following code:
var testClass = new TestClass();
testClass.MyProperty.Add(new ClassA()); testClass.MyProperty.Add(new ClassB());
var mappedClass = testClass.Adapt<TestClass,TestClass>();
the new mappedClass instance has only two items of type BaseClass in “MyProperty” instead of new instances of ClassA and ClassB. How do I configure Mapster to don’t map ClassA and ClassB to BaseClass ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7
Top Results From Across the Web
Mapping one source class to multiple derived classes with ...
AutoMapper does not support registering against derived interfaces. Only classes are handled. To make it work, you have to change your type ...
Read more >Mapping Inheritance
If for some base class you have many directly derived classes, as a convenience, you can include all derived maps from the base...
Read more >Question/problem with mapping a derived type to base type
If I map a derived class instance to a base class instance, it still returns me an instance of the derived class. Is...
Read more >Inheritance from derived classes with AutoMapper
The problem is if you try to do a Mapper.CreateMap from your source to the DERIVED class, the properties of the base class...
Read more >Inheritance - EF Core
EF can map a .NET type hierarchy to a database. This allows you to write your .NET entities in code as usual, using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yeah sorry, above code is wrong, plus I forgot that the property is list. Try this
Basically, for
Adaptmethod you can pass source type and destination type, and logic for mapping will be created once at runtime.Thank you so much! 😃