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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:21:03+00:00 2026-05-29T11:21:03+00:00

Is there a way to transform a base class into its derived class? Here

  • 0

Is there a way to transform a base class into its derived class?

Here is a simple example of two classes:

namespace BLL
{
    public class Contact 
    {
       public int ContactID { get; set; }
       public string Name { get; set; }

       public Contact(){}
    }
}

namespace BLL
{
    public class SpecialContact : Contact
    {
       public SpecialContact(){}
    }
}

Ideally, I could do something like this:

Contact contact = new Contact();
SpecialContact specialContact = new SpecialContact();

contact.ContactID = 123;
contact.Name = "Jeff";

specialContact = contact;

This code of course throws an error. Apart from writing another constructor for SpecialContact or method that sets each property, is there any other solution?

  • 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-29T11:21:04+00:00Added an answer on May 29, 2026 at 11:21 am

    It is illegal to assign Base class reference to the Derived class reference variable.

    Variable of type X can only be a reference to an object of type X or derived.

    You probably need to have another instance of SpecialContact which contains the same data as existing object. There is no way to avoid manual copying.

    I use the following extension method when I need to copy the matching properties from one object to another incompatible one(1):

    public static void AssignFrom(this object destination, object source) {
      Type dest_type = destination.GetType();
      Type source_type = source.GetType();
    
      var matching_props = from d in dest_type.GetProperties()
                            join s in source_type.GetProperties()
                            on d.Name equals s.Name
                            where d.IsWritable() && s.IsReadable()
                            select new {
                              source = s,
                              destination = d
                            };
    
      foreach (var prop in matching_props) {
        prop.destination.SetValue(destination, prop.source.GetValue(source, null), null);
      }
    }
    

    Then you can do:

    specialContact.assignFrom(contact);
    

    Please consider this a workaround. The proper solution would be to design properly your class hierarchy where you do not come to this problem.


    1 Note: it matches the properties by name and assumes they are of the same type.

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

Sidebar

Related Questions

Is there simple way transform one object in another, first defined in application object
I am wondering if there is a way to transform a matrix of 2
There must be a generic way to transform some hierachical XML such as: <element1
Is there a way to make OpenGL transform a general vector I give it
Is there a way to compute efficiently the Fourier transform of the max of
Is there a way to transfer a new class instance (python class that inherits
Is there a way to DRY this CSS up? Only difference is color? div.base-text-gold
Is there a easy way to transform 1000000 in 1.000.000? A regex or string
I am using XSLT to transform XML. Is there a way for the XSLT
I'm stuck here... Is there a way we can get results from MySQL database

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.