How can I serialize an object where the object has a Delegate created from an Action?
<p>How can I serialize an object where the object has a <code>Delegate</code> created from an <code>Action</code> such that I can deserialize the object and the <code>Delegate</code> is still subscribed?</p>

<p>Here is the class code:</p>

<pre><code>[Serializable]
public class TestClass
{
    public event EventHandler&lt;EventArgs&gt; refresh;
    public void AddHandlerForAction()
    {
        var methodInfo = this.GetType().GetMethod("RefreshMethodNoParamaters");
        Action action = (Action)Delegate.CreateDelegate(typeof(Action), this, methodInfo);

        var eventInfo = this.GetType().GetEvent("refresh");

        var handlerType = eventInfo.EventHandlerType;
        var eventParams = handlerType.GetMethod("Invoke").GetParameters();

        var parameters = eventParams.Select(p =&gt; Expression.Parameter(p.ParameterType, "x"));
        var body = Expression.Call(Expression.Constant(action), action.GetType().GetMethod("Invoke"));
        var lambda = Expression.Lambda(body, parameters.ToArray());
        Delegate d = Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false);

        eventInfo.AddEventHandler(this, d);
    }
    public virtual object Clone()
    {
        Type type = this.GetType();
        if (!type.IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        if (Object.ReferenceEquals(this, null))
        {
            return this.GetType().GetMethod("GetDefaultGeneric").MakeGenericMethod(type).Invoke(this, null);
        }
        using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
            stream.Position = 0;
            var clone = Convert.ChangeType(formatter.Deserialize(stream), type);
            return clone;
        }
    }
    public void RaiseEvent()
    {
        EventHandler&lt;EventArgs&gt; eventHandler = refresh;
        if (eventHandler != null)
        {
            eventHandler(this, new EventArgs());
        }
    }
    public void RefreshMethodNoParamaters()
    {

    }
}
</code></pre>

<p>Here is the code that performs an exception:</p>

<pre><code>var testClass = new TestClass();
testClass.AddHandlerForAction();
testClass.RaiseEvent();

var clone = testClass.Clone();
</code></pre>

<p>The exception occurs at this line of code:</p>

<pre><code>formatter.Serialize(stream, this);
</code></pre>

<p>Here is the exception:</p>

<blockquote>
  <p>An unhandled exception of type
  'System.Runtime.Serialization.SerializationException' occurred in
  mscorlib.dll</p>
  
  <p>Additional information: Cannot serialize delegates over unmanaged
  function pointers, dynamic methods or methods outside the delegate
  creator's assembly.</p>
</blockquote>

<p>The objective of the code is to subscribe to an <code>Event</code> where the method that is called when the <code>Event</code> is raised has no parameters.</p>