Skip to main content

Get class

Get by class name

You can get an IClass object from a class name by using the GetClass method of the IMetamodels object.

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

//Get the class name of the model currently displayed in the editor
IModel model = c.App.Workspace.CurrentModel;
IClass cls = metamodels.GetClass(model.ClassName);
c.App.Output.WriteLine("sample", $"Class: {cls.DisplayName}");

//Get all fields of "Actor"
IFieldCollection fields = cls.GetFields();
foreach (IField field in fields)
{
if (field.Name.StartsWith("___") || field.Name.StartsWith("$"))
{
//Ignore system fields
continue;
}
c.App.Output.WriteLine("sample", $"Field: {field.DisplayName}");
}
}

Note

When retrieving profile elements using the API, system-defined elements can also be retrieved at the same time. System-defined elements follow the following rules, so exclude them if not needed.

  • Class
  • Fully qualified name (IClass.FullName) prefixed with "System."
  • Name (IClass.Name) prefixed with "___"
  • Field
  • Name (IField.Name) prefixed with "$"
  • Name (IField.Name) prefixed with "___"

Retrieve from model

To retrieve from model, use the Metaclass property of the IModel object.

public void GetClassFromModel(ICommandContext c, ICommandParams p) 
{
//Get the model
IModel model = c.App.Workspace.CurrentModel;

//You can get IClass from the Metaclass property
IClass cls = model.Metaclass;
}