Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need help converting between similar types

Need help converting between similar types

Scheduled Pinned Locked Moved C#
helpcsharpcssclouddata-structures
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik Westermann
    wrote on last edited by
    #1

    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 to PersonEx and from PersonEx to Person. I don't mind loosing the information in PersonEx.theDetails when converting to Person and leaving PersonEx.theDetails null when converting from a Person 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

    W C 2 Replies Last reply
    0
    • E Erik Westermann

      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 to PersonEx and from PersonEx to Person. I don't mind loosing the information in PersonEx.theDetails when converting to Person and leaving PersonEx.theDetails null when converting from a Person 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

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • W Wendelius

        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

        E Offline
        E Offline
        Erik Westermann
        wrote on last edited by
        #3

        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

        W P 2 Replies Last reply
        0
        • E Erik Westermann

          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

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Okay, I see. Based on my experiences it wouldn't take many lines of code to copy property values from class to another as long as property names and their types match. Happy coding :) Mika

          1 Reply Last reply
          0
          • E Erik Westermann

            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

            P Offline
            P Offline
            Paw Jershauge
            wrote on last edited by
            #5

            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

            P E 2 Replies Last reply
            0
            • P Paw Jershauge

              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

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              I hate to say it but your sig has a typo (well 2).

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              P 1 Reply Last reply
              0
              • P Pete OHanlon

                I hate to say it but your sig has a typo (well 2).

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                P Offline
                P Offline
                Paw Jershauge
                wrote on last edited by
                #7

                What do you mean ?

                With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\

                P 1 Reply Last reply
                0
                • P Paw Jershauge

                  What do you mean ?

                  With greate code, comes greate complexity, so keep it simple stupid...:-\ :-\

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  There's only 1 e in great.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  P 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    There's only 1 e in great.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    P Offline
                    P Offline
                    Paw Jershauge
                    wrote on last edited by
                    #9

                    :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...:-\ :-\

                    P 1 Reply Last reply
                    0
                    • P Paw Jershauge

                      :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...:-\ :-\

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Perfect.

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      1 Reply Last reply
                      0
                      • E Erik Westermann

                        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 to PersonEx and from PersonEx to Person. I don't mind loosing the information in PersonEx.theDetails when converting to Person and leaving PersonEx.theDetails null when converting from a Person 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

                        C Offline
                        C Offline
                        carbon_golem
                        wrote on last edited by
                        #11

                        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

                        E 1 Reply Last reply
                        0
                        • P Paw Jershauge

                          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

                          E Offline
                          E Offline
                          Erik Westermann
                          wrote on last edited by
                          #12

                          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

                          1 Reply Last reply
                          0
                          • C carbon_golem

                            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

                            E Offline
                            E Offline
                            Erik Westermann
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups