By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,708 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,708 IT Pros & Developers. It's quick & easy.

GetType of passed object

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try:

Dim xs As XmlSerializer = New XmlSerializer(config.GetType)
P.S.
Make sure the object & sub objects are marked as Serializable.
Schneider

"Matthew" <tu*************@alltel.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #2

P: n/a
> Dim xs As XmlSerializer = New XmlSerializer(config.GetType)

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

Matthew
Nov 21 '05 #3

P: n/a
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.Ge tType)
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
Nov 21 '05 #4

P: n/a
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.GetTy pe)
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.GetTy pe, 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

"Matthew" <tu*************@alltel.net> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
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.Ge tType)
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

Nov 21 '05 #5

P: n/a
Schneider, thanks for the examples. They helped solve my problem.

Matthew
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.