473,395 Members | 1,488 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,395 software developers and data experts.

Serialization Surrogates

I have a class that contains a field that is a System.Drawing.Pen. I need to
be able to serialize the class but since the .NET Framework does not have
the Pen marked as serializable I must create a surrogate class that handles
the serialization instead. So here's my class:

NotInheritable Class PenSerializationSurrogate

Implements ISerializationSurrogate

Public Sub GetObjectData(ByVal obj As Object, ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements
System.Runtime.Serialization.ISerializationSurroga te.GetObjectData
Dim p As Drawing.Pen = CType(obj, Drawing.Pen)
info.AddValue("colour", p.Color)
info.AddValue("width", p.Width)
End Sub

Public Function SetObjectData(ByVal obj As Object, ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext, ByVal selector As
System.Runtime.Serialization.ISurrogateSelector) As Object Implements
System.Runtime.Serialization.ISerializationSurroga te.SetObjectData
Dim p As Drawing.Pen = CType(obj, Drawing.Pen)
p.Color = CType(info.GetValue("colour", GetType(Drawing.Color)),
Drawing.Color)
p.Width = CType(info.GetValue("width", GetType(Single)), Single)
Return Nothing
End Function

End Class

However I'm unsure how I get my class to always use the surrogate when it is
serialised. There is an example here:

http://msdn.microsoft.com/msdnmag/is...?fig=true#fig2

....but it looks like I have to register the surrogate with the formatter
that does the serialization. However, I want this to be more automatic! I
want this class to use this surrogate every time ANY formatter tries to
serialize it. Is this possible?

TIA
May 25 '06 #1
1 3159
Is there anything that you can do to Refactor you class so that the Pen field
does not have to be serialized?

e.g. Add a Color and Width properties and then add a read=only property that
constructs and returns a pen instance.

The central point to serialization is that you want to maindain/persist/copy
a state, not necessarily an object graph, per se.

"elziko" wrote:
I have a class that contains a field that is a System.Drawing.Pen. I need to
be able to serialize the class but since the .NET Framework does not have
the Pen marked as serializable I must create a surrogate class that handles
the serialization instead. So here's my class:

NotInheritable Class PenSerializationSurrogate

Implements ISerializationSurrogate

Public Sub GetObjectData(ByVal obj As Object, ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements
System.Runtime.Serialization.ISerializationSurroga te.GetObjectData
Dim p As Drawing.Pen = CType(obj, Drawing.Pen)
info.AddValue("colour", p.Color)
info.AddValue("width", p.Width)
End Sub

Public Function SetObjectData(ByVal obj As Object, ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext, ByVal selector As
System.Runtime.Serialization.ISurrogateSelector) As Object Implements
System.Runtime.Serialization.ISerializationSurroga te.SetObjectData
Dim p As Drawing.Pen = CType(obj, Drawing.Pen)
p.Color = CType(info.GetValue("colour", GetType(Drawing.Color)),
Drawing.Color)
p.Width = CType(info.GetValue("width", GetType(Single)), Single)
Return Nothing
End Function

End Class

However I'm unsure how I get my class to always use the surrogate when it is
serialised. There is an example here:

http://msdn.microsoft.com/msdnmag/is...?fig=true#fig2

....but it looks like I have to register the surrogate with the formatter
that does the serialization. However, I want this to be more automatic! I
want this class to use this surrogate every time ANY formatter tries to
serialize it. Is this possible?

TIA

May 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Brown | last post by:
In mid-October 2004, Jeff Epler helped me here with this string iterator: def chars(s): """ This generator function helps iterate over the characters in a string. When the string is unicode and...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
27
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the...
0
by: elziko | last post by:
In the past when doing binary serialization I have been able to implement my own serialization for a type by defining a serialization surrogate for this type. I am now working on something...
17
by: Adam Olsen | last post by:
As was seen in another thread, there's a great deal of confusion with regard to surrogates. Most programmers assume Python's unicode type exposes only complete characters. Even CPython's own...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
3
by: Sin Jeong-hun | last post by:
Hi. There are a server and a client, and I need to exchange "Message" objects between them. The class Message included in both applications has the exact signature (namespace, all fields,...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.