Hello,
I am trying to store an object and its type in a database table as
text and then restore the object to its original form. I have created
a couple of functions to help me with this:
'Takes an object as a parameter.
'Returns the XML representation of the object.
Public Shared Function ObjectToXMLString(ByVal [Object] As Object) As
String
Dim xs As Xml.Serialization.XmlSerializer
Dim ms As New IO.MemoryStream
xs = New Xml.Serialization.XmlSerializer([Object].GetType)
xs.Serialize(ms, [Object])
Dim result As String =
System.Text.Encoding.ASCII.GetString(ms.ToArray())
Return result
End Function
Public Shared Function XMLStringToObject(ByVal XMLString As String,
ByVal Type As System.Type) As Object
'Create a serializer of the proper type
Dim xs As New Xml.Serialization.XmlSerializer(Type)
'Create an XML document to read the xml string
Dim xd As New Xml.XmlDocument
xd.LoadXml(XMLString)
'Save the result of the XML document into a memory stream
Dim ms As New System.IO.MemoryStream
xd.Save(ms)
'read from the memorystream
Dim Bytes(ms.Length - 1) As Byte
ms.Read(Bytes, 0, ms.Length)
ms.Position = 0
Return xs.Deserialize(ms)
End Function
These functions change an object to XML and XML to an object
respectively (and are quite handy). However, the second function
requires a type (as System.Type) in order to convert the XML into an
object. This is where the problem lies, because I am saving the type
into the database as a string (i.e. "MyNamespace.MyType"). I would
like to create an overload to the XMLStringToObject function that
accepts a string TypeName as the second parameter.
I have made several attempts to use the GetType methods of Type and
Object to try to get a System.Type from the persisted string, but it
always returns nothing as pointed out in the included post. I know
there MUST be a way to do this because this is exactly what the XML
Serialization does - converts the string types back into datatypes.
Does anybody know how to do this?
NOTE: One of the replies to the following post suggested to remove the
quotes from the type - but this is exactly the problem I have - I need
to convert a string into a System.Type.
PREVIOUS POST:
Hi every body.
Im new to VB.NET (wrote C++ all my life) and Im facing the
following problem. I created a dll with a new class name A
in it and then a Module that has reference to that dll. I
am able to create the object of type A in the Module but
when tring to use the system GetType function to i
get "nothing". the following is my code. Can you tell me
what am I doing wrong.
--------dll code--------------------------------------
Namespace xxx
Public Class A
Function SaySomething() As String
SaySomething = "HELLO"
End Function
End Class
End Namespace
-------------------------------------------------------
Then i created another vb project and add a reference to
the prev dll.
-----------------Module code---------------------------
Module Module1
Sub Main()
Dim Obj As xxx.A 'this goes well...
Dim T As System.Type
Try
T = Type.GetType("xxx.A") 'get "nothing" ???
Catch ex As Exception
Console.WriteLine("Excaption")
End Try
End Sub
End Module
---------------------------------------------
Thanks.