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

  • Home
  • SEARCH
  • 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 3678320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:19:37+00:00 2026-05-19T03:19:37+00:00

So I have two custom complex types like this (oversimplified for this example): public

  • 0

So I have two custom complex types like this (oversimplified for this example):

public class PendingProduct
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int ColorCount { get; set; }
}

Let’s say I need this object to know how to convert itself to another type:

public class Product
{
    public string Name { get; set; }
    public string Description { get; set; }
    public ProductColors Colors { get; set;}
}

So when I call a method to convert PendingProduct to Product, I’ll execute some custom logic that adds “ColorCount” number of ProductColor objects to the Product class. This is totally oversimplified, but the architecture of the class is really irrelevant here.

What my main question is, is this:

What’s the best practice method to use to implement the conversion of one complex type to another complex type when the properties of the objects differ?

In the real world, the properties are very different and I will be writing some custom logic to map what I need from Object A to Object B.

Obviously I could just write a function which takes an input parameter of Object A and returns Object B, but I’m looking for a more “Best Practice” method. Does IConvertible come into play here? Is there something more OOP-like that I can take advantage of rather than just writing a function to do what I want?

Object A should always know how to convert itself to Object B.

EDIT: As a side note, in the real world, Object A and Object B are both Entity Framework 4 entities. I want to take a “Pending Product”, convert it to a new Product entity, attach it to the data context and save.

  • 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-19T03:19:38+00:00Added an answer on May 19, 2026 at 3:19 am

    There are numerous ways this can be done, but they really boil down to either writing the mapping code yourself, handling it through reflection, or relying on a pre-built framework like AutoMapper. I’ve answered a similar question in another SO question here.

    I’ll add it here for your reference:

    Realistically you could

    1.Reflection

    public void Map<TSource, TDestination>(TSource source, TDestination destination)
    {
      var props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
      var type = typeof(TDestination);
    
      foreach (var prop in props)
      {
        object value = prop.GetValue(source, null);
    
        var prop2 = type.GetProperty(prop.Name);
        if (prop2 == null)
          continue;
    
        if (prop.PropertyType != prop2.PropertyType)
          continue;
    
        prop2.SetValue(destination, value, null);
      }
    }
    

    2.Copy Constructor

    public Employee(Person person)
    {
      // Copy properties
    }
    

    3.Implicit/Explicit Conversion

    public static implicit operator Employee(Person person)
    {
      // Build instance and return
    }
    

    4.AutoMapper

    Mapper.Map<Person, Employee>(person);
    

    5.Combination of 3/4:

    public static implicit operator Employee(Person person)
    {
      return Mapper.Map<Person, Employee>(person);
    }
    

    A note on implicit/explicit conversion operators: I believe in using these you won’t be generating CLS-compliant code.

    AutoMapper does allow you to customise how different properties on the target type are mapped over, e.g.:

    Mapper.CreateMap<Person, Employee>()
          .ForMember(e => e.Forename, o => o.MapFrom(p => p.Forename.ToLower()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two custom controls that are analogous to a node and the control
I have two lists of custom objects and want to update a field for
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two classes, and want to include a static instance of one class
I have written a custom Silverlight control based on Control. I have two DependencyProperties
I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two identical tables and need to copy rows from table to another.
I have two threads, one needs to poll a bunch of separate static resources

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.