1

I am having trouble with deep-copying a list.

My main window, called Form1, has an instance of System.Collections.Generic.List<T> and I wish to provide the ability to edit this list through a separate SettingsForm which has an OK- and a Cancel-button. If the user presses "OK" then the changes to the list shall take effect but if the user presses "Cancel" then (naturally) all changes must be dismissed. To achieve this SettingsForm is meant to make a deep copy of the list, make any changes the user requests to the copied list, and then - if the user presses "OK - hand the edited copy back to Form1 to replace the original.

To make the deep copy I am using the MemoryStream-BinaryFormatter solution suggested here and my problem is that when I call up SettingsForm a second time I get an exception from my deep-copy function stating that null is not serializable. It is as if I had passed null to the deep-copy function but I have taken care not to.

Well, if you've read this far I'm guessing that you would like to see some code, so here we go; first the relevant parts of Form1:

public partial class Form1 : Form
{
...
private List<ScriptListEntry> scriptList;
...
public Form1()
{
  InitializeComponent();
  ...

  // Create an empty script list
  scriptList = new List<ScriptListEntry>();
  ...
}
...

private void toolStripButton2_Click(object sender, EventArgs e)
{
  /* Will display 'SettingsForm' to allow the user to change the program
   * settings.
   */

  ...

  SettingsForm sform = new SettingsForm();

  ...

  sform.setScriptList(scriptList);

  sform.ShowDialog();
}



Then SettingsForm

public partial class SettingsForm : Form
{
...

private List<ScriptListEntry> scriptList;
private List<ScriptListEntry> scriptListWorkingCopy;

public SettingsForm()
{
  InitializeComponent();

  scriptList = null;
  scriptListWorkingCopy = null;
  ...
}

public void setScriptList(List<ScriptListEntry> scriptList_)
{
  // Keep a reference to the original list
  scriptList = scriptList_;

  if (null != scriptList_)
  {
    if (0 != scriptList_.Count)
    {
      /* Make a working copy because settings made in 'SettingsForm' must
       * not be committed until OK-button is clicked. 'DeepCopy' does not
       * work if object to be copied is 'null' or the list is empty. 
       */
      scriptListWorkingCopy = DeepCopy<List<ScriptListEntry>>.deepCopy(scriptList_);

      ...
    }
  }
  else
  ...
}

...

// OK-button
private void button1_Click(object sender, EventArgs e)
{
  ...

  // Update the script list
  if (null != scriptList)
  {
    scriptList.Clear();
    scriptList.AddRange(scriptListWorkingCopy);
  }
  else
  ...
}



And finally the deep-copy-machine, line 11 below throws the exception

public static class DeepCopy<T>
{
  /* Used for deep copying anything.
   * Class 'T' must have [SerializableAttribute]
   */
  public static T deepCopy(object objectToCopy)
  {
    using (MemoryStream memoryStream = new MemoryStream())
    {
      BinaryFormatter binaryFormatter = new BinaryFormatter();
      binaryFormatter.Serialize(memoryStream, objectToCopy);  // << Exception thrower
      memoryStream.Seek(0, SeekOrigin.Begin);
      return (T)binaryFormatter.Deserialize(memoryStream);
    }
  }
}

Creating the list for the first time works just fine (I've verified its contents) but when I try to bring up 'SettingsForm' a second time the exception occurs. The exception is 'System.Runtime.Serialization.SerializationException' - "PublicKeyToken=null' is not marked as serializable"

Thank you for reading!

Community
  • 1
  • 1
jokki
  • 251
  • 5
  • 13

1 Answers1

2

You have trimmed the error message text - PublicKeyToken=null is the end of the assembly-qualified name of a type - it should look something like Foo.Bar, Baz, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null - that is, class Foo.Bar in assembly Baz of version 1.2.3.4, culture-neutral, not signed. In your case, the error is that said type isn't attributed as [Serializable].

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • I see, so the "PublicKeyToken=null" does not mean that I passed 'null' to the function, that makes sense! And you are right, I had missed to give 'ScriptListEntry' the attribute [Serializable]. It works now, thanks a lot! – jokki Jan 05 '10 at 08:14