|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
why I can serialize a class without using [Serializable]?somewhere that if you want a class to be serializble you must add an attribute called [Serializable] before the class and within the namespace,I didn't do that but I could serialize it using the following code: try { XmlSerializer s = new XmlSerializer( typeof( EnviroVars) ); s.Serialize( fs, enviromental_Vars ); } catch(Exception ee) { MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source); } finally { fs.Close(); } thanks for your help [Serializable] is for runtime serialization.
Your code uses XML Serialization. XML and Runtime serialization are different mechanisms, and they behave differently , and are governed by different attributes. The XML Serializer ignores the [Serializable] attribute. -Dino Show quote "ALI-R" <new***@microsoft.com> wrote in message news:uhNRdjozEHA.1652@TK2MSFTNGP11.phx.gbl... >I have written a class in C# and I have serilized into a file .I read > somewhere that if you want a class to be serializble you must add an > attribute called [Serializable] before the class and within the > namespace,I > didn't do that but I could serialize it using the following code: > try > > { > > XmlSerializer s = new XmlSerializer( typeof( EnviroVars) ); > > s.Serialize( fs, enviromental_Vars ); > > } > > catch(Exception ee) > > { > > MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source); > > } > > finally > > { > > fs.Close(); > > } > > > > thanks for your help > > thanks for the info.so interesting
Show quote "Dino Chiesa [Microsoft]" <din***@online.microsoft.com> wrote in message news:%23y723yR0EHA.1260@TK2MSFTNGP12.phx.gbl... > [Serializable] is for runtime serialization. > > Your code uses XML Serialization. > XML and Runtime serialization are different mechanisms, and they behave > differently , and are governed by different attributes. > > The XML Serializer ignores the [Serializable] attribute. > > -Dino > > > "ALI-R" <new***@microsoft.com> wrote in message > news:uhNRdjozEHA.1652@TK2MSFTNGP11.phx.gbl... > >I have written a class in C# and I have serilized into a file .I read > > somewhere that if you want a class to be serializble you must add an > > attribute called [Serializable] before the class and within the > > namespace,I > > didn't do that but I could serialize it using the following code: > > try > > > > { > > > > XmlSerializer s = new XmlSerializer( typeof( EnviroVars) ); > > > > s.Serialize( fs, enviromental_Vars ); > > > > } > > > > catch(Exception ee) > > > > { > > > > MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source); > > > > } > > > > finally > > > > { > > > > fs.Close(); > > > > } > > > > > > > > thanks for your help > > > > > > |
|||||||||||||||||||||||