Domanda

When I put an instance of a custom class into Session and then pull it out, I need it to come out as a COPY of what's in Session, not a reference to what's in Session. Here's what I have, watered down for example purposes.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Company selectedCompany = new Company("1234"); //a company code
    selectedCompany.AnotherClass.Value1 = "hello";
    Session["OLD.Company"] = selectedCompany;

    Company newCompany = (Company)Session["OLD.Company"]; //I want this to be a COPY of what's in Session, not a reference to it.
    newCompany.AnotherClass.Value1 = "goodbye";
    Session["NEW.Company"] = newCompany;
}

I stepped through and watched the Session variables, and the above code results in the AnotherClass.Value1 for BOTH OLD.Company and NEW.Company being set to "goodbye."

Initial Google searches point me in the direction of implementing IClonable on my Company class. I tried the following, but to no avail:

public class Company : ICloneable
{
    //properties...
    //constructors...
    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

and then...

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Company oldCompany = (Company)Session["OLD.Company"];
    Company newCompany = (Company)oldCompany.Clone();
    newCompany.AnotherClass.Value1 = "goodbye";
    Session["NEW.Company"] = newCompany;
}

still results in Value1 for BOTH OLD.Company and NEW.Company being "goodbye". Now I suspect this is because MemberwiseClone() creates a "shallow" copy, and my problem here is that Value1 is a value in a property which is a reference type (AnotherClass).

But at the same time, I also found this site which says DO NOT implement ICloneable. So for my purpose here I'm not really sure what to do/what advice to pursue.

Several other sites I found show some version of this:

public static object CloneObject(object obj)
{
    using (MemoryStream memStream = new MemoryStream())
    {
        BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        binaryFormatter.Serialize(memStream, obj);
        memStream.Seek(0, SeekOrigin.Begin);
        return binaryFormatter.Deserialize(memStream);
    }
}

This requires that I make my class serializable --- which is probably okay (I'll have to read up on Serialization), but after reading the article about not using ICloneable, I'm not sure if I should invest time in pursuing an ICloneable solution.

È stato utile?

Soluzione

Your problem has nothing to do with Session objects. You just need to make a copy of an object right?

Here's how to write a copy constructor:

http://msdn.microsoft.com/en-US/library/ms173116%28v=VS.80%29.aspx

class Company 
{
...
  public Company (Company other)
  { 
    // copy fields here....
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top