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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:33:53+00:00 2026-05-16T17:33:53+00:00

My question is shown in this code I have class like that public class

  • 0

My question is shown in this code

I have class like that

public class MainCS
{
  public int A;
  public int B;
  public int C;
  public int D; 
}

public class Sub1
{
  public int A;
  public int B;
  public int C;
}


public void MethodA(Sub1 model)
{
  MainCS mdata = new MainCS() { A = model.A, B = model.B, C = model.C };   

  // is there a way to directly cast class Sub1 into MainCS like that    
  mdata = (MainCS) model;    
}
  • 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-16T17:33:54+00:00Added an answer on May 16, 2026 at 5:33 pm

    What he wants to say is:

    “If you have two classes which share most of the same properties you can cast an object from class a to class b and automatically make the system understand the assignment via the shared property names?”

    Option 1: Use reflection

    Disadvantage : It’s gonna slow you down more than you think.

    Option 2: Make one class derive from another, the first one with common properties and other an extension of that.

    Disadvantage: Coupled! if your’re doing that for two layers in your application then the two layers will be coupled!

    Let there be:

    class customer
    {
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int age { get; set; }
    }
    class employee
    {
        public string firstname { get; set; }
        public int age { get; set; } 
    }
    

    Now here is an extension for Object type:

    public static T Cast<T>(this Object myobj)
    {
        Type objectType = myobj.GetType();
        Type target = typeof(T);
        var x = Activator.CreateInstance(target, false);
        var z = from source in objectType.GetMembers().ToList()
            where source.MemberType == MemberTypes.Property select source ;
        var d = from source in target.GetMembers().ToList()
            where source.MemberType == MemberTypes.Property select source;
        List<MemberInfo> members = d.Where(memberInfo => d.Select(c => c.Name)
           .ToList().Contains(memberInfo.Name)).ToList();
        PropertyInfo propertyInfo;
        object value;
        foreach (var memberInfo in members)
        {
            propertyInfo = typeof(T).GetProperty(memberInfo.Name);
            value = myobj.GetType().GetProperty(memberInfo.Name).GetValue(myobj,null);
    
            propertyInfo.SetValue(x,value,null);
        }   
        return (T)x;
    }  
    

    Now you use it like this:

    static void Main(string[] args)
    {
        var cus = new customer();
        cus.firstname = "John";
        cus.age = 3;
        employee emp =  cus.Cast<employee>();
    }
    

    Method cast checks common properties between two objects and does the assignment automatically.

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

Sidebar

Related Questions

I have a web control that looks like this public class Foo : WebControl
I have a file named common.cs which contains commonly used functions like this public
I have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I have a problem in a WPF application. I wrote this code: public partial
Long introduction, question is at the end: Assume I have a base class that's
My question involves this simple walkthrough shown in the article Preserve Size and Location
In this question it has been shown how to use neat boolean variables in
This question shows the usage of **var = new Object(); and that's something totally
See this question. How to show popup message like in stackoverflow I am trying
For this question, let us assume that we will want to show the face

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.