By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,962 Members | 2,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,962 IT Pros & Developers. It's quick & easy.

Serialize ignoring NonSerializableAttribute

P: n/a
Joe
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
Sep 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Joe
OK - it turns out the sample is working but my actual application does
something similar and doesn't work.

"Joe" <jb*******@noemail.noemailwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
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

Sep 27 '06 #2

P: n/a
Joe
Never mind. There was a field in the base class the same as in the class
being serialized. The base class field did not have the NonSeriailzed
attribute.

"Joe" <jb*******@noemail.noemailwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
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

Sep 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.