473,382 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

GetType of passed object

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
5 2244
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
> Dim xs As XmlSerializer = New XmlSerializer(config.GetType)

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

Matthew
Nov 21 '05 #3
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
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
Schneider, thanks for the examples. They helped solve my problem.

Matthew
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Marco | last post by:
Hi. I am trying to get a type object using GetType. For this, I use: Type type = Type.GetType("System.Windows.Forms.TextBox"); But after this line, when I check, type is null. What am I...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
4
by: Howard Kaikow | last post by:
For the code below, for both appWord and gappWord, I get the error "Public member 'GetType' on type 'ApplicationClass' not found" I realize the test for appWord is superflous as the parameter...
1
by: dcs | last post by:
Hi, Can someone please help. I have a class that contains the following Structure (called MyStructure) and sub (called MainSub). And this 1st class inherits a 2nd class that contains a sub called...
6
by: Paul | last post by:
I have two projects in one solution. One is called Frontier and holds all my base user controls, classes, etc. that are used over multiple applications. The second is my application project...
3
by: Ron M. Newman | last post by:
Hi, I have a certain assembly that needs to create instances of objects where the object class is passed as a string (e.g. "System.Drawing.Bitmap"). I have found that calling "GetType" on this...
1
by: Kristian Frost | last post by:
Hello, Sorry for polluting the VB newsgroup with curly brackets, but I couldn't think how to get that question onto one line otherwise. Anyway, I'm trying to write a Sub, or Function which takes...
4
by: Steph | last post by:
hello, i want to find the type from a generic... like : public static john<T>(object obj, T myControl) { ((myControl.getType())myControl).OnClientClick ="code"; } because my generic var can...
2
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.