Yes, the SDK is a bit ambiguous with regards to the "marked as Serialzable"
references that seem to pop up in it from time to time. They are misleading.
In fact, either adding a Serializable attribute or not adding one does
nothing to make a class more or less serialzable. It is important, however,
in some cases, as the .Net platform has mechanisms for doing serialization
by using reflection.
Serialization, in this case, refers to converting data to a stream of text.
It can also refer to converting it to a binary stream, but that's not the
issue here, as we're talking about ViewState, which is text in an HTML
document. Some existing .Net classes are serialzable "straight out of the
box" (as it were). These include primtives and value types, integers,
strings, dates, you know what I'm talking about. Basically, any type that
has a ToString() method implemented which converts it to a string
representation of its Data is serializable. A few more complext types are
also "natively" serializable, including arrays of the previous types, some
Collections, and conveniently, DataSets and DataTables. These are types that
implement the ISerializable interface. For most other types, and custom
types, you will probably have to implement your own custom Serialization.
This is not necessarily difficult, however, with the Serialization classes
in the Framework, depending upon exactly what you want to serialize, and how
you want to serize and deserialize it.
One of the best (and easiest) ways to serialize a class is using the
System.Xml.Serialization NameSpace. Serializtion to and from XML is not only
one of the easiest things to do in .Net, but it also happens to be possibly
the best way to serialize data to text. XML is powerful stuff. An XML
document can be transformed on the fly to any format you wish, using XSLT,
and can include any type of data as well. It is cross-platform compatible,
and a highly popular world-wide standard.
The main class that you employ in the System.Xml.Serialization namespace is
the XmlSerializer. This class is used for both serialization and
deserialization, and has a really nice API. In most cases, you can serialize
an object by simply creating an instance of the XmlSerializer class, and
calling the Serialize method. All public fields, and public properties that
implement a setter and a getter are serialized automatically, using
Reflection. Methods are not. Note that the class must implement a
parameterless constructor to be able to do this. However, you can certainly
overload the constructor to your heart's content.
The methods are restored when you deserialize the class. You can read more
about XML serialization at:
http://msdn.microsoft.com/library/de...ialization.asp
You can read more about serialization without XML at:
http://msdn.microsoft.com/library/de...bjserializ.asp
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
"Ben Amada" <be*@REpoMOweVErpick.com> wrote in message
news:ee*************@TK2MSFTNGP15.phx.gbl...
I've created a class that I need to store in ViewState. However when I
try to store it in ViewState, I get the following error:
"The type 'solution.pe2' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate."
I've included the <Serializable()> attribute, but I'm still getting the
same error.
The class is below ... as you can see it contains a Collection, two
Hashtables and an Enum.
How can I make this class and all of its contents serializable so
ViewState can store it??
TIA, Ben
-----------------------------------------
<Serializable()> Public Class pe2
Implements IEnumerable
Private m_colChildren As New Collection
Public Style As New Hashtable
Public Attributes As New Hashtable
Private m_strID As String
Private m_enumElementType As ElementType
Public Enum ElementType
Span
Div
Image
End Enum
Public Function GetEnumerator() As _
System.Collections.IEnumerator Implements _
System.Collections.IEnumerable.GetEnumerator
Return m_colChildren.GetEnumerator
End Function
End Class