M
Michael Maes
Hello,
I have a BaseClass and many Classes which all inherit (directly) from the BaseClass.
One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk.
1. The Deserialization goes like:
#Region " Load "
Public Function Load(ByVal path As String) As Object
Return DeserializeXML(className, path)
End Function
Public Function Load(ByVal path As String, ByVal user As String) As Object
Return DeserializeXML(className, path, user)
End Function
#End Region
The "problem" with this approach is that when a class get's instanciated it is returned as an Object instead a the Derived type so I have to Cast it (option strict on):
Dim Setup As New Namespace.Settings.Setup
Setup = DirectCast(Setup.Load(PathInfo.Settings), Namespace.Settings.Setup)
If I could return it with the right Type the code would be reduced to:
Dim Setup As New Namespace.Settings.Setup
Setup = Setup.Load(PathInfo.Settings)
One way to do this is to put the Load-Method in each derived Class (but that wouldn't be very OO), so I'd like to Dynamicaly (genericaly) Change the Return-Type of the Load-Method.
Only: I haven't got a clue how-to!
2. The Deserialization goes like:
#Region " Persist "
Public Sub Persist(ByVal path As String)
Try
SerializeXML(Me, className, path)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub Persist(ByVal path As String, ByVal user As String)
Try
SerializeXML(Me, className, path, user)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
#End Region
The following code is in a Module (in the same project)
Private Sub PerformSerialization(ByVal o As Object, ByVal callerClass As String, ByVal path As String, ByVal user As String)
.........
.........
Dim swFile As StreamWriter = New IO.StreamWriter(fileName)
Dim t As Type
Dim pi As PropertyInfo()
Dim p As PropertyInfo
Select Case callerClass.
Case "settings.setup"
t = GetType(Setup)
.........
.........
Case "settings.mailserver"
t = GetType(MailServer)
.........
.........
Case "settings.products"
t = GetType(Products)
End Select
pi = t.GetProperties()
' Encrypt Strings
For Each p In pi
If p.PropertyType.ToString = "System.String" Then I also think this syntax is "Not Done". What would be the Best Syntax?
p.SetValue(o, Crypto.EncryptString(p.GetValue(o, Nothing)), Nothing)
End If
Next
' Save to Disk
xmlSerializer = New Xml.Serialization.XmlSerializer(t)
xmlSerializer.Serialize(swFile, o)
swFile.Close()
End Sub
Not very OO hum!
Instead of passing "an Object" I would like to pass the Type of the Inheriting Class. But how do I "know" that in the BaseClass.
Any clues would be greatly appreciated.
Michael
PS: I use vs.Net 2003
I have a BaseClass and many Classes which all inherit (directly) from the BaseClass.
One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk.
1. The Deserialization goes like:
#Region " Load "
Public Function Load(ByVal path As String) As Object
Return DeserializeXML(className, path)
End Function
Public Function Load(ByVal path As String, ByVal user As String) As Object
Return DeserializeXML(className, path, user)
End Function
#End Region
The "problem" with this approach is that when a class get's instanciated it is returned as an Object instead a the Derived type so I have to Cast it (option strict on):
Dim Setup As New Namespace.Settings.Setup
Setup = DirectCast(Setup.Load(PathInfo.Settings), Namespace.Settings.Setup)
If I could return it with the right Type the code would be reduced to:
Dim Setup As New Namespace.Settings.Setup
Setup = Setup.Load(PathInfo.Settings)
One way to do this is to put the Load-Method in each derived Class (but that wouldn't be very OO), so I'd like to Dynamicaly (genericaly) Change the Return-Type of the Load-Method.
Only: I haven't got a clue how-to!
2. The Deserialization goes like:
#Region " Persist "
Public Sub Persist(ByVal path As String)
Try
SerializeXML(Me, className, path)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub Persist(ByVal path As String, ByVal user As String)
Try
SerializeXML(Me, className, path, user)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
#End Region
The following code is in a Module (in the same project)
Private Sub PerformSerialization(ByVal o As Object, ByVal callerClass As String, ByVal path As String, ByVal user As String)
.........
.........
Dim swFile As StreamWriter = New IO.StreamWriter(fileName)
Dim t As Type
Dim pi As PropertyInfo()
Dim p As PropertyInfo
Select Case callerClass.
Case "settings.setup"
t = GetType(Setup)
.........
.........
Case "settings.mailserver"
t = GetType(MailServer)
.........
.........
Case "settings.products"
t = GetType(Products)
End Select
pi = t.GetProperties()
' Encrypt Strings
For Each p In pi
If p.PropertyType.ToString = "System.String" Then I also think this syntax is "Not Done". What would be the Best Syntax?
p.SetValue(o, Crypto.EncryptString(p.GetValue(o, Nothing)), Nothing)
End If
Next
' Save to Disk
xmlSerializer = New Xml.Serialization.XmlSerializer(t)
xmlSerializer.Serialize(swFile, o)
swFile.Close()
End Sub
Not very OO hum!
Instead of passing "an Object" I would like to pass the Type of the Inheriting Class. But how do I "know" that in the BaseClass.
Any clues would be greatly appreciated.
Michael
PS: I use vs.Net 2003