BaseClass + DerivedClass + Serialization - Dirty
我有一个BaseClass,其中有许多类派生。
简而言之:BaseClass提供功能(方法)和派生类使用Properties扩展它。
其中一个(基本)功能是(De)序列化对象。
反序列化在BaseClass中执行>
朋友函数PerformDeserialization(ByVal path As String,ByVal user as String)As Object
Debug.WriteLine(" Settings.BaseSettings.PerformDese rialization")
如果不是path.EndsWith(" \")那么路径+ =" \"
如果用户是Nothing那么user = Me.ProductFamily
Dim cls As Object
如果File.Exists(路径+用户+ FilePrefix + CallerClass +" .xml")= True则
Dim srFile As StreamReader = New StreamReader( path + user + FilePrefix + CallerClass +" .xml")
Dim t As Type
Dim pi As PropertyInfo()
Dim p如PropertyInfo
尝试
xmlSerializer =新的Xml.Serialization.XmlSerializer(Me.GetType())
cls = xmlSerializer.Deserialize(srFile)
t = cls.GetType
pi = t.GetProperties()
每个p in pi
如果p。 PropertyType.Equals(GetType(System.String))然后
p.SetValue(cls,Crypto.DecryptString(CType(p.GetValue(cls,Nothing),String)),Nothing)
结束如果
下一页
Catch ex As Exception
Debug.WriteLine(ex.ToString):Beep()
最后
srFile.Close()
结束尝试
否则
Dim ClassType As Type = Me.GetType()
cls = Activator.CreateInstance(ClassType,True)
结束如果
InstanceHashCode = cls.GetHashCode
InstanceCreated = True
MarkClean(cls)
返回cls
结束功能
An对象由派生类中的属性集标记为DIRTY。 (例如)
私有_connectionName为字符串
公共属性ConnectionName()为字符串
获取
返回_connectionName
结束获取
设置(ByVal值为字符串)
Debug.WriteLine(" Settings.TestData.Set_ConnectionN ame)
如果_connectionName = Value则退出物业
_connectionName =价值:MarkDirty(我)
结束集
结束物业>
序列化通过Public-Method''Object''调用.Stosist。
在Sub我检查Flag IsDirty(BaseClass中的函数)以确定Object *是否应该*是否已序列化。
私有mIsDirty为布尔值= False
公共可覆盖ReadOnly属性IsDirty()为布尔值
获取
返回mIsDirty
结束获取
结束财产
问题是反序列化后(我叫了MarkClean(cls) ))所有属性似乎都被重新设置(所以他们Mar kDirty再次)。这导致我的对象总是很脏。我似乎无法有效地在MarkClean中找到一个Point(在(基础)类内)。
我的猜测是这是序列化行为。
有没有人有任何想法?
TIA,
Michael
Hi,
I have a BaseClass from which many Classes Derive.
In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties.
One of the (Base) Functionalities is to (De)Serialize the Objects.
Deserialization is performed in the BaseClass
Friend Function PerformDeserialization(ByVal path As String, ByVal user As String) As Object
Debug.WriteLine("Settings.BaseSettings.PerformDese rialization")
If Not path.EndsWith("\") Then path += "\"
If user Is Nothing Then user = Me.ProductFamily
Dim cls As Object
If File.Exists(path + user + FilePrefix + CallerClass + ".xml") = True Then
Dim srFile As StreamReader = New StreamReader(path + user + FilePrefix + CallerClass + ".xml")
Dim t As Type
Dim pi As PropertyInfo()
Dim p As PropertyInfo
Try
xmlSerializer = New Xml.Serialization.XmlSerializer(Me.GetType())
cls = xmlSerializer.Deserialize(srFile)
t = cls.GetType
pi = t.GetProperties()
For Each p In pi
If p.PropertyType.Equals(GetType(System.String)) Then
p.SetValue(cls, Crypto.DecryptString(CType(p.GetValue(cls, Nothing), String)), Nothing)
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString) : Beep()
Finally
srFile.Close()
End Try
Else
Dim ClassType As Type = Me.GetType()
cls = Activator.CreateInstance(ClassType, True)
End If
InstanceHashCode = cls.GetHashCode
InstanceCreated = True
MarkClean(cls)
Return cls
End Function
An object is marked DIRTY by the Set of the Properties in the Derived Classes. (eg)
Private _connectionName As String
Public Property ConnectionName() As String
Get
Return _connectionName
End Get
Set(ByVal Value As String)
Debug.WriteLine("Settings.TestData.Set_ConnectionN ame")
If _connectionName = Value Then Exit Property
_connectionName = Value : MarkDirty(Me)
End Set
End Property
Serialization is called through the Public-Method ''Object''.Persist.
In that Sub I check the Flag IsDirty (Function in BaseClass) to determine if the Object *should* be Serialized or not.
Private mIsDirty As Boolean = False
Public Overridable ReadOnly Property IsDirty() As Boolean
Get
Return mIsDirty
End Get
End Property
The problem is that after Deserialization (I have Called MarkClean(cls)) all the properties seem to be re-Set (So they MarkDirty again). This is causing my Objects to be Always dirty. I can''t seem to find a Point (within the (base)Class) to MarkClean effectively.
My guess is that this is Serialization-Behavior.
Does anyone have any ideas?
TIA,
Michael
嗨迈克尔
目前我正在寻找可以帮助你的人。我们将在这里回复
,并尽快提供更多信息。
如果您对此有任何疑问,请随时在此处发布。
感谢您的理解!
祝你好运,
Gary Chang
Microsoft在线合作伙伴支持/>
安全! - www.microsoft.com/security
此帖子原样是按原样提供的。没有保证,也没有赋予任何权利。
--------------------
Hi Michael
Currently I am looking for somebody who could help you on it. We will reply
here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.
Thanks for your understanding!
Best regards,
Gary Chang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
嗨Gary,
感谢您的回复。
我正在寻找有关此事的任何建议。
亲切的问候,
Michael
Hi Gary,
Thanks for your reply.
I''m looking foreward for any advise on this matter.
Kind regards,
Michael
嗨迈克尔,
>
我认为我们可以尝试在PerformDeserialization开头的
开始时暂时将TrackDirty设置为false,并在结尾处将其设置为true
PerformDeserialization ,所以deserialize程序不会调用
MarkDirty。
你可以尝试让我知道结果。
祝你好运,
Peter Huang
微软在线合作伙伴支持
安全! - www.microsoft.com/security
此帖子原样是按原样提供的。没有保证,也没有授予任何权利。
Hi Michael,
I think we can try to set the TrackDirty to be false temporarily at the
beginning of PerformDeserialization and set it back to true at the end of
PerformDeserialization, so that the deserialize procedure will not call the
MarkDirty.
You may have a try and let me know the result.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.