By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,717 Members | 2,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,717 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.