Skip to main content

Get a class's fields

Get by specifying the field name

To get a field from a class by specifying the field name, use the GetField method of the IClass object.

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

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

//Get the field of "Use case"
var field = cls.GetField("MainUseCases");

//Output the type name of the field
c.App.Output.WriteLine("sample", $"Field: {field.Type}");
}

Get a list of fields

To get a list of fields from a class, use the GetFields method of the IClass object.

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

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

//Get the fields of the class of "Actor"
IFieldCollection fields = cls.GetFields();

foreach (var field in fields)
{
//Exclude system fields
if (field.Name.StartsWith("___") || field.Name.StartsWith("$") || field.HasTags("System,System.Core")) continue;

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

Determine if a field is system-defined

When you retrieve a field using the API, you can also retrieve system-defined fields at the same time. System-defined fields follow the following rules, so exclude them if you do not need them.

  • The name (IField.Name) is prefixed with "$"
  • The name (IField.Name) is prefixed with "___"
  • The field tag is set to System or System.Core

It is useful to prepare a method like the one below.

private bool IsSystemField(IField field) 
{
//Exclude system fields
return field.Name.StartsWith("___") || field.Name.StartsWith("$") || field.HasTags("System,System.Core");
}

You can use them as follows.

IClass myClass = ... 

//Get non-system fields
var fields = myClass.GetFields().Where( f=> !IsSystemField(f) );

Get your own fields

If you want to get only the fields defined in your own class, use the DeclaredFields property of the IClass object.

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

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

//Get the fields defined in the "Actor" class
IFieldCollection declaredFields = cls.DeclaredFields;

//Get all fields in the "Actor" class excluding fields defined in the class itself
IEnumerable<IField> fields = cls.GetFields().Except(declaredFields);

//Output the fields defined in the "Actor" class excluding system fields
foreach (var field in declaredFields)
{
//Exclude system fields
if (field.Name.StartsWith("___") || field.Name.StartsWith("$") || field.HasTags("System,System.Core")) continue;

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

//Output all fields of the "Actor" class, excluding fields defined in the class itself and system fields.
foreach (var field in fields)
{
//Exclude system fields.
if (field.Name.StartsWith("___") || field.Name.StartsWith("$") || field.HasTags("System,System.Core")) continue;

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

Get all fields including those inherited

To display all fields including those inherited, you can use the Fields property in addition to the GetFields method of the IClass object. The IClass.Fields property allows you to get a dictionary of IDictionary<string, IField>. The field name is set as the Key of the dictionary, and the field object is set as the Value, so you can specify the field name to get it after getting all the fields.

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

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

//Get the fields of the class of "Actor"
IDictionary<string, IField> fields = cls.Fields;

foreach (var field in fields)
{
//Exclude system fields
if (field.Name.StartsWith("___") || field.Name.StartsWith("$") || field.HasTags("System,System.Core")) continue;

c.App.Output.WriteLine("sample", $"Field: {field.Key}");
c.App.Output.WriteLine("sample", $"Field: {field.Value.DisplayName}");
}
}