Skip to main content

Determine the model type

To determine the model type, use the Is method or As method of the IModel object.

Determine the type

The Is method of the IModel object strictly determines whether the model matches the specified type. If it is a derived class, it will return false.

public void CheckType(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Strictly check if the type of model is the "Actor" class
var isActor = model.Is("Actor");

//If it is "Actor"
if (isActor)
{
c.App.Output.WriteLine("sample", $"Model: {model.Name} is an instance of the Actor class.");
}
else //If it is not "Actor" (this also applies to derived classes)
{
c.App.Output.WriteLine("sample", $"Model: {model.Name} is not an instance of the Actor class.");
}
}

You can use the IsIn method to check multiple types.

public void CheckType(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Determines whether the type of model is the "Actor" class or the "UseCase" class.
var isActorOrUseCase = model.IsIn("Actor,UseCase");

//...
}

You can also verify by passing an array of IEnumerable<string>.

public void CheckType(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Determine whether the model type is the "Actor" class or the "UseCase" class.
var isActorOrUseCase = model.IsIn(new List<string>() { "Actor", "UseCase" });

//...
}

Determining the type including inheritance relationships

To determine whether the model is a specific class or a derived class, use the As method of the IModel object.

public void TypeAs(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Determine if the model is "Actor" or a derived class.
var asActor = model.As("Actor");

if (asActor)
{
c.App.Output.WriteLine("sample", $"Model: {model.Name} is an instance of the Actor class or a derived class.");
}
else
{
c.App.Output.WriteLine("sample", $"Model: {model.Name} is not an instance of the Actor class or a derived class.");
}
}