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

G

Guest

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
 
T

Tim Haughton

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 said:
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");
 
G

Guest

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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top