473,395 Members | 1,856 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,395 software developers and data experts.

Creating System::Type* using GetType() and String*

Hi everyone,

I'm currently writing an application which uses the XmlSerializer class to
serialize/deserialize objects to/from xml.

Now when deserializing an XmlDocument back into the object, I'm using the
System::Type::GetType(String* typeName) to create a Type* needed to construct
the XmlSerializer.

The typeName argument is taken from the XmlDocument and consists of the
Namespace of the class and the class name (i.e. "System.Type"). I add this as
an XmlRootAttribute to the class because XmlSerializer will typically
serialize objects with only the class name and not the namespace, which is
required for GetType(). I then retrieve this String when deserializing the
object and use it to create a Type*.

This has been working fine until I began to pass the XmlSerializer objects
which were not from the same namespace as the class which is making the
System.Type.GetType() call. When I call the System.Type.GetType() it returns
null, even though the type is known by the class and the class also includes
the namespace the type is declared in.

For example:

namespace namespace1
{
[XmlRootAttribute("namespace1.serializeClass")]
public __gc class serializeClass
{
//Data members, constructors etc...
};

}

//Another File:
#using "namespace1.dll"

namespace namespace2
{
using namespace namespace1;
[XmlRootAttribute("namespace2.serializeClass2")]
public __gc class serializeClass2
{
//Data members, constructors, etc...
};

public __gc class serializer()
{
System::Type* type1 = System::Type::GetType("namespace2.serializeClass2" );
//Will return a valid System.Type

System::Type* type2 = System::Type::GetType("namespace1.serializeClass") ;
//Will return null
}
}

Does anyone have any ideas as to why this might be happening?

Cheers
Johnny
Jul 22 '05 #1
2 3451
You could use the overload that takes a bool to see if there are any
exceptions that might help you.

One question though - are these two classes in the same assembly?

Regards,

Tim Haughton

"JohnnySparkles" <Jo************@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi everyone,

I'm currently writing an application which uses the XmlSerializer class to
serialize/deserialize objects to/from xml.

Now when deserializing an XmlDocument back into the object, I'm using the
System::Type::GetType(String* typeName) to create a Type* needed to construct the XmlSerializer.

The typeName argument is taken from the XmlDocument and consists of the
Namespace of the class and the class name (i.e. "System.Type"). I add this as an XmlRootAttribute to the class because XmlSerializer will typically
serialize objects with only the class name and not the namespace, which is
required for GetType(). I then retrieve this String when deserializing the
object and use it to create a Type*.

This has been working fine until I began to pass the XmlSerializer objects
which were not from the same namespace as the class which is making the
System.Type.GetType() call. When I call the System.Type.GetType() it returns null, even though the type is known by the class and the class also includes the namespace the type is declared in.

For example:

namespace namespace1
{
[XmlRootAttribute("namespace1.serializeClass")]
public __gc class serializeClass
{
//Data members, constructors etc...
};

}

//Another File:
#using "namespace1.dll"

namespace namespace2
{
using namespace namespace1;
[XmlRootAttribute("namespace2.serializeClass2")]
public __gc class serializeClass2
{
//Data members, constructors, etc...
};

public __gc class serializer()
{
System::Type* type1 = System::Type::GetType("namespace2.serializeClass2" ); //Will return a valid System.Type

System::Type* type2 = System::Type::GetType("namespace1.serializeClass") ;
//Will return null
}
}

Does anyone have any ideas as to why this might be happening?

Cheers
Johnny

Jul 22 '05 #2
No, they are in different assemblies, which is the problem.

I've worked out my problem, I needed to add the assembly name to the string
being used to create the type. Unoftunately the XmlSerializer doesn't
serialize this properly so I need to find some sort of work around for this.
It adds these __x0020 tags or something, any idea how to turn it off?

Cheers
Johnny

"Tim Haughton" wrote:
You could use the overload that takes a bool to see if there are any
exceptions that might help you.

One question though - are these two classes in the same assembly?

Regards,

Tim Haughton

"JohnnySparkles" <Jo************@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi everyone,

I'm currently writing an application which uses the XmlSerializer class to
serialize/deserialize objects to/from xml.

Now when deserializing an XmlDocument back into the object, I'm using the
System::Type::GetType(String* typeName) to create a Type* needed to

construct
the XmlSerializer.

The typeName argument is taken from the XmlDocument and consists of the
Namespace of the class and the class name (i.e. "System.Type"). I add this

as
an XmlRootAttribute to the class because XmlSerializer will typically
serialize objects with only the class name and not the namespace, which is
required for GetType(). I then retrieve this String when deserializing the
object and use it to create a Type*.

This has been working fine until I began to pass the XmlSerializer objects
which were not from the same namespace as the class which is making the
System.Type.GetType() call. When I call the System.Type.GetType() it

returns
null, even though the type is known by the class and the class also

includes
the namespace the type is declared in.

For example:

namespace namespace1
{
[XmlRootAttribute("namespace1.serializeClass")]
public __gc class serializeClass
{
//Data members, constructors etc...
};

}

//Another File:
#using "namespace1.dll"

namespace namespace2
{
using namespace namespace1;
[XmlRootAttribute("namespace2.serializeClass2")]
public __gc class serializeClass2
{
//Data members, constructors, etc...
};

public __gc class serializer()
{
System::Type* type1 = System::Type::GetType("namespace2.serializeClass2" );

//Will return a valid System.Type

System::Type* type2 = System::Type::GetType("namespace1.serializeClass") ;
//Will return null
}
}

Does anyone have any ideas as to why this might be happening?

Cheers
Johnny


Jul 22 '05 #3

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

Similar topics

4
by: Jamie B | last post by:
Is it possible to use System.Type, Activator or whatever to create a complete "blank", unitialized object of a given type, and then at a later time call a constructor on it. The only caveat is that...
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
1
by: Josh | last post by:
Hi Guys, I have hit a wall with the my project... The problem is that i need to get the type of a usercontrol that is not in the assembly of the page that is tryiong to call it. if anyone knows of...
5
by: Don | last post by:
I have an array of System.type and I need to go through the array and perform different logic depending on the type stored in the array. I want to do: if (typeof typeCollection(i) is String)...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
1
by: ceestand | last post by:
I'm trying to make a change to an object depending contextually on it's type. The following code, even though I pass the Int32 into the function as an Object, still knows it's an Int32. public...
3
by: Nathan Sokalski | last post by:
I have the following code: Dim values As New ArrayList() values.Add("Yes") values.Add("No") values.Add("Maybe") values.Add("Whatever") dim x as String()=values.ToArray() However, I recieve...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.