How do you make a copy of an object?

I created the Colors class. I set certain properties in the Colors object and set it in the Session variable. When I access the Session variable on another page, I notice that if I change the properties to objColors below, it changes the session and does not save the original properties that I want to do. Here is an example:

Session["Colors"] = Colors;

Colors objColors = Session["Colors"];

//If I change objColors, it changes the Session.  I don't want this to happen.

Is there a better way to keep the original properties? Why is this done?

+5
source share
10 answers

Create a copy constructor for colors. Then do it.

Colors objColors = new Colors((Colors)Session["Colors"]);

In the new constructor, just copy the values ​​you need and do other things related to the constructor.

What happens in your code is that you get a pointer Colors. Session [ "Colors" ] objColors , , , . Colors Sparkin , objColors Session [ "Colors" ].

edit: :

public Colors(Colors otherColors)
{
    this.privateVar1 = otherColors.privateVar1;
    this.publicVar2 = otherColors.publicVar2;
    this.Init();
}
+7

, "" . , , , Colors objColors = Session["Colors"]; , . , , - , . IClonable . , . MemberwiseClone , .

+3

.

:

+3

. . . . .

+2

. , , . , , , . , , , String.

, , , , .

, , , .

+2

, , ICloneable MemberwiseClone. - , . :

public class Bla : ICloneable
{
    string _someFieldToClone;

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

    public Bla Clone()
    {
        return (Bla)MemberwiseClone();
    }
}
+1

, , . # :

  public static MemoryStream Serialize(object data)
    {

        MemoryStream streamMemory = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

        formatter.Serialize(streamMemory, data);

        return streamMemory;


    }



   public static Object Deserialize(MemoryStream stream)
    {

        BinaryFormatter formatter = new BinaryFormatter();
        formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
        return formatter.Deserialize(stream);

    }

: MemoryStream. Deserialize, .

, Serializable, , Serializable .

+1

, objColors Session [ "Colors" ] - . , .

. SO Q & A :

0

Colors - , , , - , , , - :

public class Colors
{
  public int Property1;
  public int Property2;
  public int Property3;
  public Colors()
  {
    //Do your regular constructor here
  }
  public Colors(Colors colorToCopy)
  {
    this.Property1 = colorToCopy.Property1;
    this.Property2 = colorToCopy.Property2;
    this.Property3 = colorToCopy.Property3;
  }
}

( , , ), imo. , , , .

0

, .

Colors objColors = (Colors)Session["Colors"];

[ "" ] objColors , , , .

, Deep Copy . IClonable Invoice :

public class Colors: IClonable
{
  public int Red;
  public int Green;
  public int Blue;

  public object Clone()
  {
    return this.MemberwiseClone();
  }
}

You now have a real deep copy of the invoice object.

Colors objColors = (Colors)((Colors)Session["Colors"]).Clone();

Additional Information: Cloning objects using IClonable and MemberwiseClone depending on the depth of your object.

0
source

All Articles