title: Deep copying a Func within an object in C#
body: <p>I am building a list of unit tests, which are organised as a list of objects, each of which contain the test method to be executed as a Func. Each object has a variable which is within scope of the Func and is used by it. The variable is not passed in as a parameter. </p>
  
  <p>Iterating over the list and running all the tests runs fine, but is is possible to copy a Func from one object, -breaking the reference to that object-, and assign it to a new object? I assume this is possible somehow by creating a Deep Copy, but my attempt using BinaryFormatter has not worked, any tips would be appreciated!</p>
  
  <p>I have a simplified forms application as follows to illustrate my problem:</p>
  
  <pre><code>using System; 
  using System.Collections.Generic; 
  using System.ComponentModel; 
  using System.Data; 
  using System.Drawing; using
  using System.IO;
  using System.Linq; 
  using System.Runtime.Serialization.Formatters.Binary; 
  using System.Text;
  using System.Threading.Tasks; using System.Windows.Forms;
  
  namespace WindowsFormsApplication4 {
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }
  
  
  
  
       public static object DeepClone(object obj)
       {
           object objResult = null;
           using (MemoryStream ms = new MemoryStream())
           {
               BinaryFormatter bf = new BinaryFormatter();
               bf.Serialize(ms, obj);
  
               ms.Position = 0;
               objResult = bf.Deserialize(ms);
           }
           return objResult;
       }
  
       [Serializable]
       public class POCOwithFunc {
  
           public POCOwithFunc(Func&lt;string&gt; myfunc)
           {
               mqi = myfunc;
           }
  
           public POCOwithFunc() { }
  
           public Func&lt;string&gt; mqi;
  
           public object parm;
  
       }
  
  
  
       private void button1_Click(object sender, EventArgs e)
       {
  
  
           List&lt;POCOwithFunc&gt; testList = new List&lt;POCOwithFunc&gt;();
  
           for (int x = 0; x &lt; 5; x++)
           {
  
               var pc = new POCOwithFunc();
               pc.parm = x;
               pc.mqi = delegate()
          {
              var rrq = pc.parm;      
              return "result: " + pc.parm;
          };
  
               testList.Add(pc);
           }
  
           String output = "";
           foreach (var test in testList)
           {
               output += test.mqi() + "\r\n";
          }
  //output:
  //result: 0
  //result: 1
  //result: 2
  //result: 3
  //result: 4
  
  
  
  
  
           var pocoToBeCopied = testList[2];
  
           var newpoco = new POCOwithFunc();
           newpoco.parm = 10;
           newpoco.mqi = pocoToBeCopied.mqi;
  
           var res = newpoco.mqi();  //returns 2
  
  
           newpoco = (POCOwithFunc)DeepClone(pocoToBeCopied);  //fails
  
       }
  
   } }
  </code></pre>