If I serialize an object from with the same class, any fields with the
NonSerializableAttribute still get serialized but not if I serialize from
outside the class.
Why?
This is a simple case to reproduce the problem. Notice that m_table should
NOT be serialized.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Test t = new Test();
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter
bf = new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
System.IO.FileStream fs = new
System.IO.FileStream("test_external.txt", System.IO.FileMode.OpenOrCreate);
bf.Serialize(fs, t);
fs.Close();
t.Clone();
}
}
[Serializable]
public class Test : ICloneable
{
[NonSerialized]
private DataTable m_table;
public Test()
{
m_table = new DataTable();
m_table.Columns.Add("Column1");
}
#region ICloneable Members
public object Clone()
{
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter
bf = new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
System.IO.FileStream fs = new System.IO.FileStream("test.txt",
System.IO.FileMode.OpenOrCreate);
bf.Serialize(fs, this);
fs.Close();
return null;
}
#endregion
}
-Joe