Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble deep-copying a 'List<T>' C#
    primarykey
    data
    text
    <p>I am having trouble with deep-copying a list.</p> <p>My main window, called <code>Form1</code>, has an instance of <code>System.Collections.Generic.List&lt;T&gt;</code> and I wish to provide the ability to edit this list through a separate <code>SettingsForm</code> 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 <code>SettingsForm</code> 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 <code>Form1</code> to replace the original.</p> <p>To make the deep copy I am using the <code>MemoryStream</code>-<code>BinaryFormatter</code> solution suggested <a href="https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically">here</a> and my problem is that when I call up <code>SettingsForm</code> a second time I get an exception from my deep-copy function stating that <code>null</code> is not serializable. It is as if I had passed <code>null</code> to the deep-copy function but I have taken care not to.</p> <p>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 <code>Form1</code>:</p> <pre><code>public partial class Form1 : Form { ... private List&lt;ScriptListEntry&gt; scriptList; ... public Form1() { InitializeComponent(); ... // Create an empty script list scriptList = new List&lt;ScriptListEntry&gt;(); ... } ... 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(); } </code></pre> <p><br> <br> Then <code>SettingsForm</code></p> <pre><code>public partial class SettingsForm : Form { ... private List&lt;ScriptListEntry&gt; scriptList; private List&lt;ScriptListEntry&gt; scriptListWorkingCopy; public SettingsForm() { InitializeComponent(); scriptList = null; scriptListWorkingCopy = null; ... } public void setScriptList(List&lt;ScriptListEntry&gt; 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&lt;List&lt;ScriptListEntry&gt;&gt;.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 ... } </code></pre> <p><br> <br> And finally the deep-copy-machine, line 11 below throws the exception</p> <pre><code>public static class DeepCopy&lt;T&gt; { /* 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); // &lt;&lt; Exception thrower memoryStream.Seek(0, SeekOrigin.Begin); return (T)binaryFormatter.Deserialize(memoryStream); } } } </code></pre> <p>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"</p> <p>Thank you for reading!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload