Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:22+00:00 2026-05-13T09:01:22+00:00

I am having trouble with deep-copying a list. My main window, called Form1 ,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:01:22+00:00Added an answer on May 13, 2026 at 9:01 am

    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].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with the following sentence: DeepClone performs a deep clone of the
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Im having trouble using a .NET COM in vb6, It compiles ok and I
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
I've been having trouble getting my ASP.NET application to automatically log users into the
I'm having trouble reading a chunked response when using a StreamReader to read the
I'm having trouble with global variables in php. I have a $screen var set
I am having trouble integrating two products, one of which is mine and they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.