I know that you can serialize a delegate like such:
[Serializable]
public class Foo
{
public Func<int,int> Del;
}
...
public static void WriteFoo(Foo foo)
{
BinaryFormatter formatter = new BinaryFormatter();
using (var stream = new FileStream("test.bin", FileMode.Create, FileAccess.Write, FileShare.None))
{
formatter.Serialize(stream, foo);
}
}
But what will happen to objects that the function contains, for example, in this case:
int D = 10;
Func<int, int> del = x => x * x - D;
Foo f = new Foo();
f.Del = del;
WriteFoo(f);
What will happen to D
?
Will the value of D
be preserved?
What will happen if D
is an object reference?