473,399 Members | 3,919 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,399 software developers and data experts.

Get.Type returns nothing ???

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.
Nov 20 '05 #1
3 3737
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.


You might have to store the assembly qualified type name
(Type.AssemblyQualifiedName) for the type to be found later. If you
only specify a type name and not the assembly to Type.GetType, it only
searches Mscorlib.dll and the calling assembly.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2
Thanks for the prompt reply. I tested it with the AssemblyQualifiedName
property and it works great!

Unfortunately, the AssemblyQualifiedName has the undesired side effect
of putting the version information into the string (I am using a strong
named assembly). This means I will have to either ensure that no data
is persisted for that version before updating the DLL or I have to
update all of the rows in the database to reflect the version (assuming
the .NET framework won't automatically try to convert to the new version
- I think I read something about that somewhere).

I will experiment with it and see how it goes.

Thanks again,
Sven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3
Sven,
You should not need the version information.

I use "MyNamespace.MyType, MyAssembly" with Type.GetType and it finds the
type.

I use the above with MyAssembly deployed with my app in the executable's bin
folder.

Hope this helps
Jay
"Sven" <no****@somewhere.com> wrote in message
news:OU****************@tk2msftngp13.phx.gbl...
Thanks for the prompt reply. I tested it with the AssemblyQualifiedName
property and it works great!

Unfortunately, the AssemblyQualifiedName has the undesired side effect
of putting the version information into the string (I am using a strong
named assembly). This means I will have to either ensure that no data
is persisted for that version before updating the DLL or I have to
update all of the rows in the database to reflect the version (assuming
the .NET framework won't automatically try to convert to the new version
- I think I read something about that somewhere).

I will experiment with it and see how it goes.

Thanks again,
Sven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4

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

Similar topics

5
by: Arnaud Legrand | last post by:
Hello, I have a question about portability and I have not found the answer in the FAQ. I have different modules to build. All of them have the same private part and a common public part (though...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
6
by: Howard Kaikow | last post by:
I'm doing a VB 6 project in which I am trying to protect against type mismatch errors. Is the process any different in VB .NET? Here's what I'm doing in VB 6. I have an ActiveX DLL. The...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
4
by: Tom Bradford | last post by:
Let me first say that I'm sure that this subject has come up before, and so forgive me for beating a dead horse. Secondly, let me say that Python's strength is its dynamic nature, and I don't...
0
by: CrispinH | last post by:
Hi I've just upgraded some Type Editors from 1.1 to 2.0 and whilst they compile OK, I'm not getting any results in the test harness PropertyGrid. Usually when you select a particular property,...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
2
by: Jess | last post by:
Hello, I understand that if a function "f" has a local variable "a", and after it returns, "a" vanishes. If "f" returns "a" as a result, then I noticed the following: 1. if the return type is...
7
by: mirandacascade | last post by:
The questions are toward the bottom of this post. Situation is this: 1) Access 97 2) Multi-user appplication 3) SQL Server 2000 4) Sporadically (i.e. less than 1% of the time) encounter the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.