How to Serialize a class


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm trying to wrap my head around this serialization thing. I have built (the start of) a replacement (er...ok maybe replacement is a bit strong...) for the BrowseDialog (because it doesn't allow code driven filters, to enable row level security). I'd like to save search configurations, which would just be an instance of a class, which means I need to serialize the class. I have read about this, but I'm not getting it yet. How do you enable serialization of a class? A brief example might help.



Thanks!
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
To enable the serialization of a class, you need to put the SerializableAttribute on the class like this:

<Serializable()> _
Public Class MyClass
End Class

That flag tells the compiler that you can serialize the object.  Now, within the class definition, if there are fields that you do not want to serialize, then you can mark then with the NotSerializableAttribute() like this:

<NotSerializable()> _
Private _MyField As String

Then, you use the formatters within the System.Runtime.Serialization.Formatters namespace to serialize the objects to and from streams, just like the .NET serialization samples show.

Now, when you say "serialization", you might also be talking about the windows forms designer "serializing" your property values to the .designer.vb file at design-time, but that's got a whole different set of attributes that are attached to the properties on a class.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
So, if I add the Serializable() attribute to a class, then I can indicate that class as a custom data type for a field in a BO and save instances to the db?



If this is right, then I'm assuming the field type in the db would be varbinary(max) in SQL 2005 and image in 2000 right?



Am I correct that if I have referenced classes within this class, and they are also marked as Serializable(), they would also get serialized along with the parent class (and thus also stored)?



It can't be that easy....Blink
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Oh, but it is that easy Smile  Some of .NET is just magic...

Yes, all of the field types within your object will need to be serializable as well.  And unfortunately, you won't be able to tell whether all of the .NET types you're using in fields are serializable until runtime.  You never get any compile-time errors with serialization stuff.

As for the data type, yes, it needs to be either Image or VarBinary(MAX), and then you need to customize the field within the BOMapper and mark it as serializable and set the property type.

StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
There are other ways to implement serialization on a class... you can implement the ISerializable() interface, but then, you have to manually add and retrieve all of the property values from the serialization stream... more control, but also more code.  You also have to add a special constructor signature for serialization... something like Public Sub New(ByVal context As SerializationContext, ... I don't remember the rest.

However, that method of serialization goes beyond what I can tell you in a post.  You'd have to go msdn2.microsoft.com and check their documentation for ISerializable.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Thanks for the info. I'm going to try this out.
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Have fun with it Smile  There used to be some stuff you couldn't do with serialization in VB.NET that you had to use C# to accomplish (like not serializing events), but with VB.NET 2005, all of those problems have been alleviated, so you should be able to do what you're wanting to do.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search