question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping derived classes not working

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
chaowlertcommented, Nov 15, 2017

Yeah sorry, above code is wrong, plus I forgot that the property is list. Try this

TypeAdapterConfig<TestClass, TestClass>.NewConfig()
    .Map(dest => dest.MyProperty, src => src.MyProperty.Select(item => item.Adapt(item.GetType(), item.GetType()).ToList())

Basically, for Adapt method you can pass source type and destination type, and logic for mapping will be created once at runtime.

0reactions
Sascha-Lcommented, Nov 15, 2017

Thank you so much! 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found