Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6102413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:36:40+00:00 2026-05-23T13:36:40+00:00

I’d like to be able to project/map properties from either of 2 child classes.

  • 0

I’d like to be able to project/map properties from either of 2 child classes.
Imagine a parent class that has 2 child classes, either of 1 which points to a child class. Each child class must map to properties on the model class.

However, if child class 1 is null then it shouldn’t map across to the model it should map class 2 properties instead.

It’s like what I want to do here except do it across a whole class of properties rather than 1 property:

Conditonal projection

I was hoping I don’t have to create a CustomResolver for each class property and whether there is a better way of achieving this. Imagine that the child classes have 10 identical properties each, all I want to do is switch the map depending on which child class is populated.

public class Message
{
     public string Comment { get; set; }
     public Inbound? InboundMessage { get; set; }
     public Outbound? OutboundMessage { get; set; }
}

public class Inbound
{
     public string Body { get; set; }
    // 10 properties...
}

public class Outbound
{
     public string Body { get; set; }
    // 10 properties...
}


public class MessageModel
{
     public string Comment { get; set; }
     public string Body { get; set; }
    // 10 properties....
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:36:41+00:00Added an answer on May 23, 2026 at 1:36 pm

    Surprised no-one had an answer to this so answered myself by the time I worked out what to do.

    If you have the need to conditionally map properties depending on whether a child class is populated or not, or any other logic for the whole class for that matter, then you can use a TypeConverter.

    Inside the TypeConverter, you test the source parameter in the ConvertCore function, and then call Mapper.Map on the correct source to pass the child class properties out to the model.

    The return from mapper can be then returned back out which will be the destination.
    I never realised that you can still call Mapper.Map() even in the TypeConverter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.