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. How to use "Reflection "to compare classes?

How to use "Reflection "to compare classes?

Scheduled Pinned Locked Moved C#
xmlhelptutorialquestion
4 Posts 4 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.
  • L Offline
    L Offline
    Luka Grabarevic
    wrote on last edited by
    #1

    hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke

    C D 2 Replies Last reply
    0
    • L Luka Grabarevic

      hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      pdluke wrote:

      i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same.

      Be careful. The two objects will not be the same. What you mean is that you are testing to see if they are equal. You can do this without reflection. You can override the Equals method:

      public override bool Equals(object obj)
      {
      bool isEqual = base.Equals(obj);
      if (!isEqual)
      return false;

      // Do more stuff to determine equality - updating isEqual
      
      return isEqual;
      

      }

      Only you can determine what constitutes equality.


      Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      R 1 Reply Last reply
      0
      • C Colin Angus Mackay

        pdluke wrote:

        i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same.

        Be careful. The two objects will not be the same. What you mean is that you are testing to see if they are equal. You can do this without reflection. You can override the Equals method:

        public override bool Equals(object obj)
        {
        bool isEqual = base.Equals(obj);
        if (!isEqual)
        return false;

        // Do more stuff to determine equality - updating isEqual
        
        return isEqual;
        

        }

        Only you can determine what constitutes equality.


        Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

        R Offline
        R Offline
        Russell Jones
        wrote on last edited by
        #3

        if you have unit tests for your object you could use them instead of the equals method. It depends whether you are interested in them being functionally identical or having the same state or both. Russell

        1 Reply Last reply
        0
        • L Luka Grabarevic

          hi i'd like to compare 2 instances of the same class. i've used the XmlSerializer to save an instance of a class into a Xml-File. after the deserialization from the file, i'd like to compare the original instance of the class and the one i've deserialized to see if the two instance are the same. i've ask a friend and he told me that i have to use Reflection, but he didn't told me how exactlly i have to do that. pls help greets pdluke

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          Use this.GetType().GetProperties() to get an array of the properties, then us the GetValue method of each to get the value of the property for this and obj2 to compare the values. If you're not exposing all your data as properties, use GetMembers instead.

          -- Rules of thumb should not be taken for the whole hand.

          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