Tuesday, May 22, 2012

Compare Lists with custom objects, which have a common superclass

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?





No comments:

Post a Comment