3

I have a Metamodel that's built like this:

class ModelElement
{
    string id;
}

class Package : ModelElement
{
     List<Package> nestedPackages;
     List<Class> ownedClasses;
}

class Class : ModelElement
{
}

Now I've built two Models and I want to check if they're identical. I'd like to compare the ID's of the Elements and I don't want to write a method for any type of Element.

Package a; //pretend both have classes
Package b; //and nested packages
compare(a.nestedPackages, b.nestedPackages);
compare(a.ownedClasses; b.OwnedClasses);

Since Class and Package both inherit from ModelElement, both have IDs. So I want to write a Function "compare" which compares the IDs. I thought of using Generics but the generic datatype doesn't have the attribute "id". Any ideas?

AndiH
  • 612
  • 1
  • 10
  • 24

3 Answers3

4

You could look at Enumerable.SequenceEqual, combined with a custom comparer.

bool equal = Enumerable.SequenceEqual(a.nestedPackages, b.nestedPackages,
    new ModelElementComparer());

public class ModelElementComparer : IEqualityComparer<ModelElement>
{
    public bool Equals(ModelElement x, ModelElement y)
    {
        return x.id == y.id;
    }

    public int GetHashCode(ModelElement obj)
    {
        return x.id;
    }
}

Here are MSDN links to this particular SequenceEqual override and the IEqualityComparer interface.

David M
  • 71,481
  • 13
  • 158
  • 186
2

Instead of writing a compare method, you could override the Object.Equals() method on the ModelElement class

public override bool Equals(Object obj) 
{
   //Check for null and compare run-time types.
   if (obj == null || GetType() != obj.GetType()) 
      return false;
   ModelElement m = (ModelElement)obj;
   return (id == m.id);
}

You will also need to override Object.GetHashCode() as well if you do this.

figabytes
  • 303
  • 1
  • 8
  • Problem is, the Metamodel is given and I am not allowed to modify is. Otherwise a nice solution. Thank you. – AndiH May 21 '12 at 15:31
  • Be careful when overriding Equals: https://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden – rsbarro Aug 29 '18 at 21:39
0

Something like Linq's Except with a custom comparer might work here:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

An empty resulting enumerable will mean there are no differences.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187