Need help converting between similar types
-
I have two type of objects - Person and PersonEx. The difference between Person and PersonEx is that PersonEx contains an array of strings, whereas Person does not. Here's a sample:
class Person
{
public string FirstName;
public string LastName;
public string City;
}class Details
{
public string attribute1;
public string attribute2;
}class PersonEx
{
public string FirstName;
public string LastName;
public string City;
public Details[] theDetails;
}I need to convert from
Person
toPersonEx
and fromPersonEx
toPerson
. I don't mind loosing the information inPersonEx.theDetails
when converting toPerson
and leavingPersonEx.theDetails null
when converting from aPerson
is ok. So far, I have been writing helper methods that do a member-by-member copy of each field (InstanceOfPerson.FirstName = InstanceOfPersonEx.FirstName;
). This is not ideal since there are a lot of fields. I'd like to walk over each type in the source and copy the value to the target so I could write something like this...public TO_TYPE Convert< FROM_TYPE, TO_TYPE >(instanceOfFromType)
. I looked into reflection, but that can be painful. I had a similar problem when converting between two types that are identical except that the type names are different - I solved that problem through serialization, but this problem is quite different. One option is, I suppose, to continue to use serialization, and then 'fix' the resulting XML to more closely align with the type to which I am converting so that I end up with a working instance on deserialization. The types I'm converting between don't change often - they just have a lot of fields and I am looking for a more reliable means of copying common information that is better and less error prone than member-by-member copying. Any advice?Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453 -
I have two type of objects - Person and PersonEx. The difference between Person and PersonEx is that PersonEx contains an array of strings, whereas Person does not. Here's a sample:
class Person
{
public string FirstName;
public string LastName;
public string City;
}class Details
{
public string attribute1;
public string attribute2;
}class PersonEx
{
public string FirstName;
public string LastName;
public string City;
public Details[] theDetails;
}I need to convert from
Person
toPersonEx
and fromPersonEx
toPerson
. I don't mind loosing the information inPersonEx.theDetails
when converting toPerson
and leavingPersonEx.theDetails null
when converting from aPerson
is ok. So far, I have been writing helper methods that do a member-by-member copy of each field (InstanceOfPerson.FirstName = InstanceOfPersonEx.FirstName;
). This is not ideal since there are a lot of fields. I'd like to walk over each type in the source and copy the value to the target so I could write something like this...public TO_TYPE Convert< FROM_TYPE, TO_TYPE >(instanceOfFromType)
. I looked into reflection, but that can be painful. I had a similar problem when converting between two types that are identical except that the type names are different - I solved that problem through serialization, but this problem is quite different. One option is, I suppose, to continue to use serialization, and then 'fix' the resulting XML to more closely align with the type to which I am converting so that I end up with a working instance on deserialization. The types I'm converting between don't change often - they just have a lot of fields and I am looking for a more reliable means of copying common information that is better and less error prone than member-by-member copying. Any advice?Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453 -
If you can use inheritance (PersonEx : Person), it could be helpful. If not, reflection should help you to copy values. It's not so painfull at all if you take a closer look. Mika
I should have mentioned - I cannot modify the types, so setting up and inheritance hierarchy is out of the question. I'm experimenting with reflection now - I'll see how it goes. Thanks -Erik
Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453 -
I should have mentioned - I cannot modify the types, so setting up and inheritance hierarchy is out of the question. I'm experimenting with reflection now - I'll see how it goes. Thanks -Erik
Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453 -
I should have mentioned - I cannot modify the types, so setting up and inheritance hierarchy is out of the question. I'm experimenting with reflection now - I'll see how it goes. Thanks -Erik
Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453Here are some basic technics: ((PropertyInfo)yourclass).GetValue and ((PropertyInfo)yourclass).SetValue they are very easy to use... try looking them up... http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo_methods.aspx[^] Let me know if you need more help... ;)
With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\
modified on Tuesday, September 9, 2008 3:32 PM
-
Here are some basic technics: ((PropertyInfo)yourclass).GetValue and ((PropertyInfo)yourclass).SetValue they are very easy to use... try looking them up... http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo_methods.aspx[^] Let me know if you need more help... ;)
With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\
modified on Tuesday, September 9, 2008 3:32 PM
I hate to say it but your sig has a typo (well 2).
Deja View - the feeling that you've seen this post before.
-
I hate to say it but your sig has a typo (well 2).
Deja View - the feeling that you've seen this post before.
What do you mean ?
With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\
-
What do you mean ?
With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\
There's only 1 e in great.
Deja View - the feeling that you've seen this post before.
-
There's only 1 e in great.
Deja View - the feeling that you've seen this post before.
:doh: :doh: :doh: hehe sorry 4 my bad english... hehe Should be corrected now... ;)
With great code, comes great complexity, so keep it simple stupid...:-\ :-\
-
:doh: :doh: :doh: hehe sorry 4 my bad english... hehe Should be corrected now... ;)
With great code, comes great complexity, so keep it simple stupid...:-\ :-\
-
I have two type of objects - Person and PersonEx. The difference between Person and PersonEx is that PersonEx contains an array of strings, whereas Person does not. Here's a sample:
class Person
{
public string FirstName;
public string LastName;
public string City;
}class Details
{
public string attribute1;
public string attribute2;
}class PersonEx
{
public string FirstName;
public string LastName;
public string City;
public Details[] theDetails;
}I need to convert from
Person
toPersonEx
and fromPersonEx
toPerson
. I don't mind loosing the information inPersonEx.theDetails
when converting toPerson
and leavingPersonEx.theDetails null
when converting from aPerson
is ok. So far, I have been writing helper methods that do a member-by-member copy of each field (InstanceOfPerson.FirstName = InstanceOfPersonEx.FirstName;
). This is not ideal since there are a lot of fields. I'd like to walk over each type in the source and copy the value to the target so I could write something like this...public TO_TYPE Convert< FROM_TYPE, TO_TYPE >(instanceOfFromType)
. I looked into reflection, but that can be painful. I had a similar problem when converting between two types that are identical except that the type names are different - I solved that problem through serialization, but this problem is quite different. One option is, I suppose, to continue to use serialization, and then 'fix' the resulting XML to more closely align with the type to which I am converting so that I end up with a working instance on deserialization. The types I'm converting between don't change often - they just have a lot of fields and I am looking for a more reliable means of copying common information that is better and less error prone than member-by-member copying. Any advice?Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453I think I would either use specialized converter classes, custom explicit/implicit operators, or maybe some exotic kind of software pattern. I might stay away from reflection in this case. Depending on the framework version you're using you could just hammer out an extension method that converts. In any case, the real problem here is the flawed class design (I know sometimes this can't be helped). If you can merge the functionality of the Person and PersonEx classes you won't need any tricksy conversion methods. Hope that helps... Regards, Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra
-
Here are some basic technics: ((PropertyInfo)yourclass).GetValue and ((PropertyInfo)yourclass).SetValue they are very easy to use... try looking them up... http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo_methods.aspx[^] Let me know if you need more help... ;)
With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\
modified on Tuesday, September 9, 2008 3:32 PM
Paw Jershauge wrote:
Here are some basic technics:
Cool...I got it working :) I wrote a generic function that copies matching members from one type to the other. I'm new at this, so I was not able to come up with a good solution to a problem where I need to copy in indexed property. So Person and PersonEx have a member, MoreStuff, which in turn has other string members like Field1, Field2 etc. I also needed to copy the members of Person.MoreStuff to PersonEx.MoreStuff and just ended up calling my generic function with references to MoreStuff and it worked. This actually worked out well, since I can now at least make sure I copy all members between similar types. (By the way - the types are messages from BizTalk orchestrations exposed as web services. There is some wierdness with orchestrations exposed as web services, so this conversion often converts between service1.Person and service2.Person even though they are identical - only the typename is different. What a headache :) )
Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453 -
I think I would either use specialized converter classes, custom explicit/implicit operators, or maybe some exotic kind of software pattern. I might stay away from reflection in this case. Depending on the framework version you're using you could just hammer out an extension method that converts. In any case, the real problem here is the flawed class design (I know sometimes this can't be helped). If you can merge the functionality of the Person and PersonEx classes you won't need any tricksy conversion methods. Hope that helps... Regards, Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra
Reflection is working for me for now. I am using .NET 2.0 at the moment, so this might not be a problem (I think reflection had major issues in 1.0 and 1.1). I tried writing a custom converter, but there are many similar types that need conversion and they each have lots of properties. I wanted to make sure that I covered all of the properties. As for flawed class design - I agree that is the case. However, the types in question are actually exposed by BizTalk server through web services. I'd have to modify the underlying proxies to make the class design better. Since I won't be maintaining the solution in the future, I wanted to avoid having to customize auto-generated code. I should have mentioned all of this in the first place but I was too focused on first hand knowledge of the problem ;) Thanks! -Erik
Erik Westermann - wWorkflow.net - BizTalk Consulting Services
SOA * ESB * BPI * SaaS ... forget the alphabet soup - get the main course with our consulting services!
wWorkflow.net or +1 416-809-1453