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 3669458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:14:45+00:00 2026-05-19T02:14:45+00:00

I’m pretty sure this is impossible, but here goes.. I have a custom class

  • 0

I’m pretty sure this is impossible, but here goes..

I have a custom class in C# called Person which has a few properties such as Age, Height etc.

I then make a new class called Employee which inherits from Person but I don’t yet add any other properties to Employee. So its basically just a Person still, except its called an Employee.

Now say I have an instance of Person called SomePerson. How can I create a new Employee instance that has all of the values it inherited from Person, set to those inside SomePerson. Like casting from a Person into an Employee.. But without me having to manually specify each and every property that needs to be set..

Something like..

Employee NewEmployee = (Employee)SomePerson;

But of course you get the error saying “Can’t convert a Person into an Employee” etc.

Is AutoMapper the only practical solution to do things like this if you say had 300 properties in the involved objects??

UPDATE:

Auto-Mapper doesn’t seem to handle my objects..

Employee SomeEmployee = EmployeeRepository.GetEmployee(SomeEmployeeID);

// Populate the ViewModel with the Person fetched from the db, ready for editing..
VMEmployee EmployeeToEdit = Mapper.Map<Employee, VMEmployee>(SomeEmployee);


// ViewModel based on Employee with Validation applied..
[MetadataType(typeof(Employee_Validation))]
public class VMEmployee : Employee
{
    // Absolutely nothing here
}

where “Employee” is auto-generated by LINQ to SQL..

  • 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-19T02:14:45+00:00Added an answer on May 19, 2026 at 2:14 am

    AutoMapper is a good solution in this case. If you’re not going to use a property mapping framework, and you’re not willing to create a copy constructor public Employee(Person person), or an implicit/explicit conversion, how else do you expect to copy the properties across. 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.

    As @Mitch Wheat has already said, if you have an object with over 300 properties, I would reconsider what that object actually represents. Refactor refactor refactor.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6

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.