C# Copy Properties from one Object to Another.

Posted on 2012-08-22
Last Modified: 2012-08-22
Hi Experts,
i have a class as below
public class Employee
public int ID{get;set;}
public string Name{get;set;}
Employee emp = new employeee{id =1 NAme = 'aaa'}

Employee  emp1 = emp; = "BBB";

I do not want  emp's name property also to be changed to "BBB";

How can i achieve this ,  Using reflection do i need to copy properties from one object to other or is there a better way , please advice.
Question by:B_Pati
    LVL 74

    Accepted Solution

    Here's your problem:  you haven't created two objects. You've created one object with two references pointing to it. Even reflection wouldn't help you here.

    My suggestion would be to create a "copy" constructor that can initialize the object accordingly. For example:

    public Employee(Employee template)
        this.ID = template.ID;
        this.Name = "N/A";

    Open in new window

    Then you could do:

    Employee emp = new Employeee{id =1 NAme = 'aaa'}
    Employee  emp1 = new Employee(emp); = "BBB";

    Open in new window

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The copying of all the values to the new instance can be accomplished with Reflection, but I don't know that it's worth it unless you have a ~huge~ number of properties that you don't want to list out manually.  The number of lines of code will probably end up being around the same, except that you have to use a bunch of brain power use the Reflection approach and then you need to worry about bugs...
    LVL 1

    Author Closing Comment

    Thanks kaufmed: This help me with out writing reflection code.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now