How can I clone a class with all the values that are already set in it?

JC Lizard

class example:

public class Customer
{
    public int CustomerID { get; set; }
}

using the class:

    Customer customer1 = new Customer();
    customer1.CustomerID = 1;

Now how can I create a customer2 class with all the values that are stored in customer1?

MarcinJuraszek

You can do it manually:

var customer2 = new Customer { CustomerID = customer1.CustomerID };

You can implement ICloneable interface in Customer class:

public class Customer : ICloneable
{
    private int CustomerID { get; set; }

    public Customer Clone()
    {
        return new Customer { CustomerID = this.CustomerID };
    }

    object ICloneable.Clone()
    {
        return this.Clone();
    }
}

and then use it:

var customer2 = customer1.Clone();

You can serialize your object into XML/JSON and then deserialize it into new object, as described in this answer: Deep cloning objects in C#.

Or you can use reflection to get and copy all properties/fields values into your new Customer instance. It could have bad performance, but you'd have to measure it to make sure how bad it is.

Edit

One more way to do that: you can make reflection version faster using Expression Tree! Get all fields/properties and compile all necessary assignments at runtime using Expression.Lambda. After that every next Clone call will use compiled code so there will be no performance drawback at all. I've created Clone<T> extension method which does exactly that, using Expression class, static constructor and reflection. You can find the code on CodePlex: CloneExtension.cs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I set properties of a clone object?

From Dev

How can I set all negative values in a dictionary to be zero?

From Dev

How can I check all function values in PHP class?

From Dev

How can i check if a background is already set?

From Dev

How can I clone (generate code) all fields of one class into another?

From Dev

How can I clone (generate code) all fields of one class into another?

From Dev

How can I clone a git program that I already have on my computer?

From Dev

How can I clone an object and set every properties value to null?

From Dev

How can I clone an exisintg Linux VM to a scale set in Azure

From Dev

How can I add values to an already created list?

From Dev

How can I deny xdebug connections in PHPStorm that are already set as approved?

From Dev

How can I add a class to an existing li with classes already assigned to it?

From Dev

How can I set default values in Eve?

From Dev

How can I set 3 values in spinner

From Dev

How can I set 3 values in spinner

From Dev

How can I set SendAs for all people

From Dev

How can I set class name dynamically?

From Dev

How can I test if all bits are set or all bits are not?

From Dev

How to decorate all methods in a class? Can I just decorate the class?

From Dev

How can I prevent certain combinations of values in a table, derived from the values already in the table?

From Dev

How can I prevent certain combinations of values in a table, derived from the values already in the table?

From Dev

How can I set variables from a class of ArrayLists to the Main class?

From Dev

How do I clone a class in intellij?

From Dev

How do I clone a class in intellij?

From Dev

How can I set my BASH history scrolling to filter by what I've already typed?

From Dev

How can I clone only a subfolder of a Git repository and make all the files in the subfolder end up in the root?

From Dev

How can I clone a Ubuntu installation from a dual drive dual boot set up to new SSD?

From Dev

how to check if I already set uncaughtException event

From Dev

Can I force initialization for all possible enum values of an enum class class/function template?

Related Related

  1. 1

    How can I set properties of a clone object?

  2. 2

    How can I set all negative values in a dictionary to be zero?

  3. 3

    How can I check all function values in PHP class?

  4. 4

    How can i check if a background is already set?

  5. 5

    How can I clone (generate code) all fields of one class into another?

  6. 6

    How can I clone (generate code) all fields of one class into another?

  7. 7

    How can I clone a git program that I already have on my computer?

  8. 8

    How can I clone an object and set every properties value to null?

  9. 9

    How can I clone an exisintg Linux VM to a scale set in Azure

  10. 10

    How can I add values to an already created list?

  11. 11

    How can I deny xdebug connections in PHPStorm that are already set as approved?

  12. 12

    How can I add a class to an existing li with classes already assigned to it?

  13. 13

    How can I set default values in Eve?

  14. 14

    How can I set 3 values in spinner

  15. 15

    How can I set 3 values in spinner

  16. 16

    How can I set SendAs for all people

  17. 17

    How can I set class name dynamically?

  18. 18

    How can I test if all bits are set or all bits are not?

  19. 19

    How to decorate all methods in a class? Can I just decorate the class?

  20. 20

    How can I prevent certain combinations of values in a table, derived from the values already in the table?

  21. 21

    How can I prevent certain combinations of values in a table, derived from the values already in the table?

  22. 22

    How can I set variables from a class of ArrayLists to the Main class?

  23. 23

    How do I clone a class in intellij?

  24. 24

    How do I clone a class in intellij?

  25. 25

    How can I set my BASH history scrolling to filter by what I've already typed?

  26. 26

    How can I clone only a subfolder of a Git repository and make all the files in the subfolder end up in the root?

  27. 27

    How can I clone a Ubuntu installation from a dual drive dual boot set up to new SSD?

  28. 28

    how to check if I already set uncaughtException event

  29. 29

    Can I force initialization for all possible enum values of an enum class class/function template?

HotTag

Archive