Change namespace?

B

Bradley Plett

Is it possible to change the namespace of an object? If so, how?

If you want more context to this question, you can take a look at the
thread I started entitled "Shared class and namespace issues in web
service". Basically I'm trying to change the namespace of an object
returned to me by a web service to match a client class.

I assume the solution will require serialization and deserialization,
but even then I can't figure out how to strip the namespace from the
object or to change it to match.

Thanks!
Brad.
 
J

Jon Skeet [C# MVP]

Bradley Plett said:
Is it possible to change the namespace of an object? If so, how?

An object doesn't have a namespace - it has a type, which in turn has a
namespace. That's a subtle difference.

You can't change the type of an object, nor can you pretend that a type
from one namespace is actually a type from a different namespace.

As regards your issue - you could use reflection to automatically
get/set all the properties. Not terribly nice, but it should work.
You'll have to do it recursively if any of the property types have the
same problem.
 
B

Bradley Plett

Yes, I understand the differentiation. Thanks for correcting me. :)

I understand that the type of an object can't be changed, but
considering they're coming from the same piece of code, I know they're
the same thing (even if the machine doesn't). I can understand the
difficulty, given that the code could drift apart, etc., but.... I
would just think there should be some way of casting them to the same
type, since the data structure is identical.

Can you give me a code sample of the reflection method you're talking
about? Obviously I could simply assign all of the values from the one
object to the properties of the other, but I'd like to think there'd
be a more elegant way than manually going through each property.

Also, I should add an observation I've made. If I serialize the
object I get from the web service to a file, each element is tagged
with the namespace. However, if I serialize an object I create in the
client, there are NO namespace tags. Why doesn't the client object
include namespaces? Given this, if I could serialize the server
object without the namespaces (strip them off somehow), then I'd be
able to deserialize to the client object. Doesn't that make sense?

Thanks again!
Brad.
 
J

Jon Skeet [C# MVP]

Bradley Plett said:
Yes, I understand the differentiation. Thanks for correcting me. :)

I understand that the type of an object can't be changed, but
considering they're coming from the same piece of code, I know they're
the same thing (even if the machine doesn't). I can understand the
difficulty, given that the code could drift apart, etc., but.... I
would just think there should be some way of casting them to the same
type, since the data structure is identical.

That's not the way it works - they are just *completely* separate types
as far as the CLR is concerned.
Can you give me a code sample of the reflection method you're talking
about? Obviously I could simply assign all of the values from the one
object to the properties of the other, but I'd like to think there'd
be a more elegant way than manually going through each property.

I don't have time right now, but it looks (from a very brief scan) as
if

http://www.eggheadcafe.com/tutorials/aspnet/a4264125-fcb0-4757-9d78-
ff541dfbcb56/net-reflection--copy-cl.aspx

has stuff you'll want.
Also, I should add an observation I've made. If I serialize the
object I get from the web service to a file, each element is tagged
with the namespace. However, if I serialize an object I create in the
client, there are NO namespace tags. Why doesn't the client object
include namespaces? Given this, if I could serialize the server
object without the namespaces (strip them off somehow), then I'd be
able to deserialize to the client object. Doesn't that make sense?

I'm not sure it's relevant - the client could be creating objects from
a completely different. Don't forget that XML namespaces aren't the
same as .NET namespaces.

You'd have to change the client code to make it create objects from the
right namespace.

Coincidentally, I've been looking at exactly the same situation myself
recently - and concluded that returning a string and deserializing from
that was the best way to go.
 
S

Steven Cheng[MSFT]

Hi Bradley,

As I've also mentioned in your another thread, the problem you met should
be a particular case and dynamically change namespace of type would be a
quite expensive and complex solution. Do you think it ok to manually change
the autogenerated client proxy code and modify the webmethods to use your
custom class? This is much simpler.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bradley Plett

Thank-you very much! I think this put me onto the right track. I may
have found as elegant a solution as I can hope for. Yes, it may have
some issues, but it works for me! :)

// the idea for this came from
http://www.eggheadcafe.com/tutorial...d78-ff541dfbcb56/net-reflection--copy-cl.aspx
private static T SetProperties<T, U>(U fromRecord, T toRecord)
{
foreach (PropertyInfo fromField in
fromRecord.GetType().GetProperties())
{
if (fromField.Name != "Id")
{
foreach (PropertyInfo toField in
toRecord.GetType().GetProperties())
{
if (fromField.Name == toField.Name)
{
toField.SetValue(toRecord,
fromField.GetValue(fromRecord, null), null);
break;
}
}
}
}
return toRecord;
}

Thanks!
Brad.
 

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