Skip to main content

Get a list of classes

Get a list of all classes

Get all classes by using the AllClasses property of the IMetamodels object.

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

//Get all classes
IClassCollection classes = metamodels.AllClasses;

foreach (var cls in classes)
{
if(cls.FullName.StartsWith("System."))
{
//Ignore system-defined classes
continue;
}

c.App.Output.WriteLine("sample", $"Class: {cls.Name}");
}
}
Note

When you use the API to get profile elements, you can also get system-defined elements at the same time. System-defined elements follow the following rules, so exclude them if you do not need them.

  • Package
  • The fully qualified name (IPackage.FullName) is prefixed with "System."
  • Class
  • The fully qualified name (IClass.FullName) is prefixed with "System."
  • The name (IClass.Name) is prefixed with "___"
  • Field
  • The name (IField.Name) is prefixed with "$"
  • The name (IField.Name) is prefixed with "___"

Get a list of classes directly under a package

This is an example of specifying a package name and getting a list of classes directly under that package.

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

//Specify a package from all packages
var packageName = "SomePackageName";
IPackageCollection packages = metamodels.AllPackages;
IPackage targetPackage = packages.FirstOrDefault(p => p.Name == packageName);
if(targetPackage == null)
{
c.App.Output.WriteLine("sample", $"The specified package does not exist");
return;
}

//Output the names of classes directly under the package
IClassCollection classes = targetPackage.OwnedClasses;
foreach (var cls in classes)
{
if(cls.FullName.StartsWith("System."))
{
//Ignore classes defined in the system
continue;
}

c.App.Output.WriteLine("sample", $"Class: {cls.DisplayName}");
}
}

Get all classes under a package

This is an example of specifying a package name and getting all classes under that package.

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

//Specify a package from all packages
var packageName = "SomePackageName";
IPackageCollection allPackages = metamodels.AllPackages;
IPackage targetPackage = allPackages.FirstOrDefault(p => p.Name == packageName);
if(targetPackage == null)
{
c.App.Output.WriteLine("sample", "The specified package does not exist");
return;
}

//Output the names of classes directly under the package
//Get an enumeration of classes directly under the package by unioning the package and itself
IEnumerable<IPackage> packages = GetAllSubPackages(targetPackage); var classes = packages.SelectMany(p => p.OwnedClasses); foreach (var cls in classes) { if(cls.FullName.StartsWith("System.")) { //Ignore system-defined classes continue; } c.App.Output.WriteLine("sample", $"Class: {cls.DisplayName}"); } } private IEnumerable<IPackage> GetAllSubPackages(IPackage targetPackage) { if(!targetPackage.SubPackages.Any()) { return new []{ targetPackage }; return new []{ targetPackage }.Union(targetPackage.SubPackages.SelectMany(p => GetAllSubPackages(p)));
}