Check the type of a field value
To check the type of a field value from a model, first access the Metaclass
property of the IModel
object and use the IClass.GetField
method for the metaclass to get the field. For the obtained field, use the Type
property of the IField
object to get the type name of the field.
public void GetFieldType(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//For a field named "SomeField"
//Get the field from the metaclass
IField someField = model.Metaclass.GetField("SomeField");
//Get the type name
string type = someField.Type;
//...
}
For primitive types
For primitive types such as strings and integers, you can get the type name as follows.
Type | Type name |
---|---|
String | String |
Integer | Integer |
Double | Double |
Boolean | Boolean |
Rich Text Edit Tool | RichText |
For class types
For class types, you can get the class name. To get the type class as an IClass
object, use the TypeClass
property of the IField
object.
public void GetFieldTypeClass(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
IField someField = model.Metaclass.GetField("SomeField");
//Get the type class
IClass type = someField.TypeClass;
if (type == null)
{
c.App.Window.UI.ShowMessageBox("Not a class type field.");
}
//...
}
Enumerations
For enumerations, you can get the enumeration name. To get the enumeration as an IEnum object, use the TypeEnum property of the IField object.
public void GetFieldTypeEnum(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
IField someField = model.Metaclass.GetField("SomeField");
//Get the enumeration for the type
IEnum type = someField.TypeEnum;
if (type == null)
{
c.App.Window.UI.ShowMessageBox("Not an enumeration field.");
}
//...
}