0

I generated a class from xml (using xsd.exe) and now I have to compare its two instances. The class generated is:

 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
 [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable =    false)]
 public partial class ResultSet
 {
   private ResultSetResult[] resultField;

  [System.Xml.Serialization.XmlElementAttribute("Result")]
   public ResultSetResult[] Result
  {
      get
    {
        return this.resultField;
    }
    set
    {
        this.resultField = value;
     }
   }
 }

 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
     public partial class ResultSetResult
  {
    private decimal latitudeField;
    private string precisionField;

  public decimal Latitude
  {
     get
    {
        return this.latitudeField;
    }
    set
    {
       this.latitudeField = value;
     }
   }

  [System.Xml.Serialization.XmlAttributeAttribute()]
  public string precision
  {
     get
     {
        return this.precisionField;
     }
     set
     {
         this.precisionField = value;
     }
    }
   }

I know we have to implement a function but this class has partial classes, so how to compare a particular field's value ?

I am tried using

Public static bool PublicInstancePropertiesEqual<T>(T self, T to, params    string[] ignore) where T : class 
{
    if (self != null && to != null)
    {
        Type type = typeof(T);
        List<string> ignoreList = new List<string>(ignore);
        foreach (System.Reflection.PropertyInfo pi in   type.GetProperties(System.Reflection.BindingFlags.Public |   System.Reflection.BindingFlags.Instance))
        {
            if (!ignoreList.Contains(pi.Name))
            {
                object selfValue = type.GetProperty(pi.Name).GetValue(self, null);
                object toValue = type.GetProperty(pi.Name).GetValue(to, null);

                if (selfValue != toValue && (selfValue == null ||   !selfValue.Equals(toValue)))
                {
                    return false;
                }
            }
        }
        return true;
    }
    return self == to;
}

but the last line self==to gives error (Operator == can not be applied to operands of the type class_name)

David Arno
  • 42,717
  • 16
  • 86
  • 131
Novak007
  • 426
  • 1
  • 9
  • 23
  • 5
    There is nothing different from 'normal' classes and partial classes after they have been compiled. Is there something you are having trouble with something/comparing in particular? – Caramiriel Jun 05 '15 at 11:05
  • @Caramiriel so i could compare private decimal latitudeField; private string precisionField; just as they were properties of a normal class ? – Novak007 Jun 05 '15 at 11:09
  • possible duplicate of [Comparing object properties in c#](http://stackoverflow.com/questions/506096/comparing-object-properties-in-c-sharp) – Yeldar Kurmangaliyev Jun 05 '15 at 11:09
  • 1
    There isn't something like an unnormal class in c#. To Serialize something via the XMLSerializer it must be public so simply compare the public properties. – Ralf Jun 05 '15 at 11:11
  • You have two classes there - `ResultSet` and `ResultSetResult`. Is that a typo? – David Arno Jun 05 '15 at 11:11
  • @DavidArno I think he didn't meant partial but wanted to express that one class is used as Type of a property of the other class. So pressumably he wants some sort of ~deep~ compare. – Ralf Jun 05 '15 at 11:16
  • @Ralf, ah that makes sense. In that case, your comment re XML and public properties is the correct answer to this issue, so maybe you should post it as such. – David Arno Jun 05 '15 at 11:18
  • @DavidArno No it isn't a typo, its created from the xml structure. – Novak007 Jun 05 '15 at 11:19
  • @Ralf Please see the edit – Novak007 Jun 05 '15 at 11:25
  • @DavidArno Please see the edit – Novak007 Jun 05 '15 at 11:26
  • Do you want to Compare your classes or any classes? If you want to compare just instances the shown classes create another partial class part for ResultSet let it implement IComparable and do a concrete comparision in there. – Ralf Jun 05 '15 at 11:30
  • @Ralf I want to do for my class only though that code should also work , right ? – Novak007 Jun 05 '15 at 11:31
  • The `return self == to` comparision is pointless. You only reach that point if one or other are `null`, in which case just return false (unless you want null == null to be true) – David Arno Jun 05 '15 at 11:31
  • @Ralf Where should I implement the IComparable http://stackoverflow.com/questions/4188013/c-sharp-interfaces-how-to-implement-icomparable I saw this and I think making the second partial class derived from IComparable will work ? This partial class thing is hazing everything for me. – Novak007 Jun 05 '15 at 11:35
  • Do you need Comparability (smaller, greater, equal) that would be IComparable or Equality then i should be IEquatable. – Ralf Jun 05 '15 at 11:41
  • @Ralf Thanks I need only equality – Novak007 Jun 05 '15 at 11:42
  • @Ralf When I have two instances of the class, I could check the values (private decimal latitudeField; private string precisionField;) using obj1.Latitude and obj2.Precision right ? – Novak007 Jun 05 '15 at 11:48
  • Yes you could but the Serializer serializes only public Properties. So for comparing it would be sufficient to compare these. – Ralf Jun 05 '15 at 11:53

0 Answers0