Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

GetType of passed object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matthew - 13 Oct 2004 05:28 GMT
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
schneider - 13 Oct 2004 05:57 GMT
Try:

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

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

Schneider

> I have a nice little Sub that saves data in a class "mySettings" to an XML
> file.
[quoted text clipped - 17 lines]
> 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.
[quoted text clipped - 6 lines]
>
> Matthew
Matthew - 15 Oct 2004 15:54 GMT
> Dim xs As XmlSerializer = New XmlSerializer(config.GetType)

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

Matthew
Matthew - 16 Oct 2004 00:24 GMT
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
schneider - 16 Oct 2004 10:18 GMT
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

> I thought I was done, but there is one more twist.
>
[quoted text clipped - 15 lines]
>
> Matthew
Matthew - 17 Oct 2004 23:56 GMT
Schneider, thanks for the examples.  They helped solve my problem.

Matthew

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.