GetType of passed object

M

Matthew

I have a nice little Sub that saves data in a class "mySettings" to an XML
file.
I call it like so:
Dim mySettings As mySettings = New mySettings
mySettings.value1 = "someText"
mySettings.value2 = "otherText"
xmlSave("C:\folder\file.xml", mySettings)

Here is the sub:
Public Shared Sub xmlSave(ByVal path As String, ByVal config As
mySettings)
Dim xs As XmlSerializer = New XmlSerializer(GetType(mySettings))
Dim fs As FileStream = New FileStream(path, FileMode.Create)
xs.Serialize(fs, config)
fs.Close()
End Sub

I thought this worked well, but now I want to save another XML class with
the xmlSave function.
I tried the following, but there is a problem in the code:
Public Shared Sub xmlSave(ByVal path As String, ByVal config As object)
Dim xs As XmlSerializer = New XmlSerializer(GetType(config))
'the line above has an error: Type 'config' is not defined.
Dim fs As FileStream = New FileStream(path, FileMode.Create)
xs.Serialize(fs, config)
fs.Close()
End Sub

Any suggestions?

Matthew
 
S

schneider

Try:

Dim xs As XmlSerializer = New XmlSerializer(config.GetType)


P.S.
Make sure the object & sub objects are marked as Serializable.


Schneider
 
M

Matthew

Dim xs As XmlSerializer = New XmlSerializer(config.GetType)

Schneider, that's exactly what I wanted!
Thanks a million!

Matthew
 
M

Matthew

I thought I was done, but there is one more twist.

Public Function xmlLoad(ByVal path As String, _
ByVal xmlClass As Object)
Dim myObject As New mySettings
Dim xs As System.Xml.Serialization.XmlSerializer = _
New System.Xml.Serialization.XmlSerializer(xmlClass.GetType)
Dim sr As New System.IO.StreamReader(path)
' Calls the Deserialize method and casts to the object type.
myObject = CType(xs.Deserialize(sr), mySettings)
sr.Close()
Return myObject
End Function

I want to remove any mention of mySettings from the above.

Any ideas?

Matthew
 
S

schneider

Well you should seperate the Serialize and DeSerialize into different
functions.

Other tips:

Always use (Unless not possible):
Option Explicit On
Option Strict On

User DirectCast instead of Ctype when possible.

Here's some samples below (See SerializeToFile)
I also have some more advanced demos on www.CodeProject.com
Jusr look in the XML section



'Copyright © 2004 Eric Schneider

Option Explicit On
Option Strict On

Imports System
Imports System.Xml
Imports System.IO

Public NotInheritable Class PersistenceBase



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes an object to a string.
''' </summary>
''' <param name="value">Object.</param>
''' <returns>System.String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObjectToString(ByVal value As Object) As
System.String

Dim StringWriter As New System.IO.StringWriter

Try
Dim XmlSerializer As New
System.XML.Serialization.XmlSerializer(value.GetType)
XmlSerializer.Serialize(StringWriter, value)

Return StringWriter.ToString

Catch ex As Exception
Throw ex
Finally
StringWriter.Close()
End Try
End Function



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes an object to a string.
''' </summary>
''' <param name="value">Object.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>System.String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObjectToString(ByVal value As Object,
ByVal extraTypes() As System.Type) As System.String

Dim StringWriter As New System.IO.StringWriter

Try
Dim XmlSerializer As New
System.XML.Serialization.XmlSerializer(value.GetType, extraTypes)
XmlSerializer.Serialize(StringWriter, value)

Return StringWriter.ToString

Catch ex As Exception
Throw ex
Finally
StringWriter.Close()
End Try
End Function



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes a object to a file.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="value">Object. The object to serialize.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object, ByVal extraTypes() As System.Type)

Dim writer As New StreamWriter(fileName)

Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType, extraTypes)

' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)

Catch x As System.InvalidOperationException
Throw x
Finally
writer.Close()
End Try
End Sub



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes a object to a file.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="value">Object. The object to serialize.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object)

Dim writer As New StreamWriter(fileName)

Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType)

' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)

Catch x As System.InvalidOperationException
Throw x
Finally
writer.Close()
End Try
End Sub



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a file into a object.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromFile(ByVal fileName As String,
ByVal value As Object, ByVal extraTypes() As System.Type) As Object

Dim fs As New IO.FileStream(fileName, FileMode.Open)

Try
Dim w As New Xml.Serialization.XmlSerializer(value.GetType, extraTypes)
Dim g As Object = w.Deserialize(fs)

Return g

Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a file into a object.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromFile(ByVal fileName As String,
ByVal value As Object) As Object

Dim fs As New IO.FileStream(fileName, FileMode.Open)

Try
Dim w As New Xml.Serialization.XmlSerializer(value.GetType)
Dim g As Object = w.Deserialize(fs)

Return g

Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a String into a object.
''' </summary>
''' <param name="value">String.</param>
''' <param name="type">Object.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromString(ByVal value As String,
ByVal type As Object) As Object

Dim fs As New IO.StringReader(value)

Try
Dim w As New Xml.Serialization.XmlSerializer(type.GetType)
Dim g As Object = w.Deserialize(fs)

Return g

Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function



''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a String into a object.
''' </summary>
''' <param name="value">String.</param>
''' <param name="type">Object.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>


''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromString(ByVal value As String,
ByVal type As Object, ByVal extraTypes() As System.Type) As Object

Dim fs As New IO.StringReader(value)

Try
Dim w As New Xml.Serialization.XmlSerializer(type.GetType, extraTypes)
Dim g As Object = w.Deserialize(fs)

Return g

Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function

Private Sub New()

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top