フィールド値の型を調べる
モデルからフィールド値の型を調べるには、まずIModelオブジェクトのMetaclassプロパティにアクセスし、メタクラスに対してIClass.GetFieldメソッドを用いてフィールドを取得します。取得したフィールドに対し、IFieldオブジェクトのTypeプロパティを用いてフィールドの型名を取得します。
public void GetFieldType(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
// "SomeField"というフィールド名の場合
// メタクラスからフィールドを取得します
IField someField = model.Metaclass.GetField("SomeField");
// 型名を取得します
string type = someField.Type;
//...
}
プリミティブ型の場合
文字列型や整数型などのプリミティブな型の場合は、以下のような型名を取得できます。
| 型 | 型名 |
|---|---|
| 文字列型 | String |
| 整数型 | Integer |
| 実数型 | Double |
| 真偽値 | Boolean |
| リッチテキスト | RichText |
クラス型の場合
クラス型の場合は、クラス名を取得できます。型のクラスをIClassのオブジェクトとして取得する場合は、IFieldオブジェクトのTypeClassプロパティを用います。
public void GetFieldTypeClass(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
IField someField = model.Metaclass.GetField("SomeField");
// 型のクラスを取得します
IClass type = someField.TypeClass;
if (type == null)
{
c.App.Window.UI.ShowMessageBox("クラス型のフィールドではありません。");
}
//...
}
列挙型の場合
列挙型の場合は、列挙名を取得できます。列挙型をIEnumのオブジェクトとして取得する場合は、IFieldオブジェクトのTypeEnumプロパティを用います。
public void GetFieldTypeEnum(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
IField someField = model.Metaclass.GetField("SomeField");
// 型の列挙型を取得します
IEnum type = someField.TypeEnum;
if (type == null)
{
c.App.Window.UI.ShowMessageBox("列挙型のフィールドではありません。");
}
//...
}