Skip to main content

Check class inheritance relationships

Check whether a specified class is compatible

To check whether a specified class matches this class or is a derived class of this class, use the IsClassOf method of the IClass object.

public void IsClassOf(ICommandContext c, ICommandParams p) 
{
//Get the metamodels
IMetamodels metamodels = c.App.Workspace.CurrentProject.Profile.Metamodels;

//Get the class of "EntityBase"
IClass entityClass = metamodels.GetClass("EntityBase");

//Get the class of "Actor"
IClass actorClass = metamodels.GetClass("Actor");

//Get the class of "UseCase"
IClass usecaseClass = metamodels.GetClass("UseCase");

//Get the class of "RootModelBase"
IClass rootModelBase = metamodels.GetClass("RootModelBase");

//If the classes match, they are considered compatible
c.App.Output.WriteLine("sample", $"Is EntityBase compatible with EntityBase: {entityClass.IsClassOf(entityClass)}");

//If it is a derived class, it will be determined to be compatible.
c.App.Output.WriteLine("sample", $"Is Actor compatible with EntityBase: {actorClass.IsClassOf(entityClass)}");

//If it is neither of the above, it will be determined to be incompatible.
c.App.Output.WriteLine("sample", $"Is Actor compatible with UseCase: {usecaseClass.IsClassOf(actorClass)}");

//If you specify a derived class of a derived class, it will be determined to be compatible.
c.App.Output.WriteLine("sample", $"Is UseCase compatible with RootModelBase: {usecaseClass.IsClassOf(rootModelBase)}");
}

Check if a class inherits from another class

To check if a specified class inherits from this class, use the IsSuperClass method of the IClass object.

public void IsSuperClass(ICommandContext c, ICommandParams p) 
{
//Get the metamodels
IMetamodels metamodels = c.App.Workspace.CurrentProject.Profile.Metamodels;

//Get the "EntityBase" class
IClass entityClass = metamodels.GetClass("EntityBase");

//Get the "Actor" class
IClass actorClass = metamodels.GetClass("Actor");

//Get the "UseCase" class
IClass usecaseClass = metamodels.GetClass("UseCase");

//Get the "RootModelBase" class
IClass rootModelBase = metamodels.GetClass("RootModelBase");

//If the class matches, it is determined that it is not an inherited class
c.App.Output.WriteLine("sample", $"Is EntityBase an inherited class of EntityBase: {entityClass.IsSuperClass(entityClass)}");

//Determined to be an inherited class
c.App.Output.WriteLine("sample", $"Is EntityBase an inherited class of Actor: {entityClass.IsSuperClass(actorClass)}");
c.App.Output.WriteLine("sample", $"Is RootModelBase an inherited class of UseCase: {rootModelBase.IsSuperClass(usecaseClass)}");

//If the class is neither of the above, it is determined not to be an inherited class
c.App.Output.WriteLine("sample", $"Is Actor an inherited class of UseCase: {actorClass.IsSuperClass(usecaseClass)}");
}