delegate或event序列化的一个问题
有这样一个类:
<Serializable()> Public Class CustLabelProperty
Private m_iTop, m_iLeft As Integer
Private m_bSuspendPropertyChanged As Boolean
Public Delegate Sub PropertyChangedHandler()
Public Event PropertyChanged As PropertyChangedHandler
Public Sub SuspendPropertyChanged()
m_bSuspendPropertyChanged = True
End Sub
Public Sub ResumePropertyChanged()
m_bSuspendPropertyChanged = False
End Sub
Protected Sub OnPropertyChanged()
If Not m_bSuspendPropertyChanged Then
RaiseEvent PropertyChanged()
End If
End Sub
<Browsable(True), Description("左上角的Y坐标"), Category("显示")> Public Property Top() As Integer
Get
Return m_iTop
End Get
Set(ByVal Value As Integer)
m_iTop = Value
OnPropertyChanged()
End Set
End Property
<Browsable(True), Description("左上角的X坐标"), Category("显示")> Public Property Left() As Integer
Get
Return m_iLeft
End Get
Set(ByVal Value As Integer)
m_iLeft = Value
OnPropertyChanged()
End Set
End Property
End Class
假如我生成这样一个类:
Dim aCustLabelProperty As New CustLabelProperty
aCustLabelProperty.Left = 10
这时序列化是可以的,序列化代码如下:
Dim BFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim StreamFile As FileStream
StreamFile = New FileStream("d:\aaa.xxx", FileMode.Create)
With BFormatter
.FilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Low
.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
.Serialize(StreamFile, aCustLabelProperty)
End With
StreamFile.Close()
但是生成CustLabelProperty后如果我加上一句:
AddHandler m_CustLabelProperty.PropertyChanged, AddressOf UpdateLabelProperty
即给PropertyChanged赋值了。 由于UpdateLabelProperty所在类是不可以序列化的,这时序列化就会出错,错误为
UpdateLabelProperty 所在的类未标记为可序列化。
但是<noserialize()>标记又不可以用于event域,不知道哪位高人指点下该怎么处理这样的情况。