473,382 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Serialize ignoring NonSerializableAttribute

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
2 2294
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
3
by: GoodMorningSky | last post by:
I have long term question about object serialization. Object serialization is used in many ways. In .net I heard XML is used for object serialization. I understand how object values are serialized...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
9
by: Gillard | last post by:
i get an exeption and i do not know what else to do to continue Dim sdf As New SaveFileDialog With sdf .AddExtension = True .DefaultExt = ".record" .FileName = Now.ToLongDateString .Filter =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.