X++ 反射运行时函数

注释

社区兴趣团体现已从 Yammer 迁移到Microsoft Viva Engage。 若要加入 Viva Engage 社区并参与最新讨论,请填写 “请求访问财务和运营 Viva Engage 社区 ”表单,然后选择要加入的社区。

本文介绍反射运行时函数。

classIdGet

检索初始化对象所属的类的数字标识符(类 ID)。

int classIdGet(class object)

参数

参数 Description
对象 要为其获取类 ID 的对象。

返回值

指定对象的类 ID。

Example

static void classIdGetExample(Args _args)
{
    int i;
    WorkTimeCheck w;

    i = classIdGet(w);
    print "Class ID for object is " + int2Str(i);
}

dimOf

检索已在 X++ 数组中为其分配空间的索引元素数。

int dimOf(anytype object)

参数

参数 Description
对象 要确定其维度大小的数组。

返回值

如果 对象 参数的值是数组,则数组中的元素数;否则为 0 (零)。

注解

dimOf 函数适用于声明为以下 X++ 基元类型的 X++ 数组:

  • 布尔
  • date
  • int
  • int64
  • real
  • utcDateTime

例如 int iAmounts[6];。 如果枚举值和扩展数据类型的数组最终基于上述基元数据类型之一(如 int),则它们也受支持。 dimOf 函数不接受所有 X++ 基元类型的数组。 以下是 dimOf 函数不接受的数组类型:

  • str
  • 容器
  • anytype
  • 类对象的数组
  • Array 类的实例

Example

static void JobDimOfArrays(Args _args)
{
    int iAmounts[20], iCounts[];
    ABCModel enumAbcModel[22]; // Enum
    ABCModelType exdtAbcModelType[24]; // Extended data type
    anytype anyThings[26];
    str sNames[28];
    Array myArrayObj; // Class

    info("Start of job.");
    info("--(Next, normal int array, dimOf() accepts it.)");
    info(int2Str(dimOf(iAmounts)));
    info("--(Next, normal enum array, dimOf() accepts it.)");
    info(int2Str(dimOf(enumAbcModel)));
    info("--(Next, normal extended data type array (based on enum), dimOf() accepts it.)");
    info(int2Str(dimOf(exdtAbcModelType)));
    info("--(Next, dynamic int array, dimension not yet set.)");
    info(int2Str(dimOf(iCounts)));
    info("--(Next, dynamic int array, after dimension established.)");

    iCounts[13] = 13;
    info(int2Str(dimOf(iCounts)));
    info(" == == == == == (Next, array types that dimOf() does not support.)");
    info("--(Next, normal anytype array, dimOf() always returns 0.)");
    info(int2Str(dimOf(anyThings)));
    info("--(Next, an instance of class X++ Array, dimOf() always returns 0.)");

    myArrayObj = new Array(Types::Integer);
    myArrayObj.value(1,501);
    info(int2Str(dimOf(myArrayObj)));
    info("--(Next, the lastIndex method provides size information about Array instances.)");
    info(int2Str(myArrayObj.lastIndex()));
    info("--(Next, normal str array, dimOf() does not accept it, job is halted.)");
    info(int2Str(dimOf(sNames)));
    info("End of job.");

}
/************  Actual Infolog output
Message (11:10:06 am)
Start of job.
--(Next, normal int array, dimOf() accepts it.)
20
--(Next, normal enum array, dimOf() accepts it.)
22
--(Next, normal extended data type array (based on enum), dimOf() accepts it.)
24
--(Next, dynamic int array, dimension not yet set.)
0
--(Next, dynamic int array, after dimension established.)
16
== == == == == (Next, array types that dimOf() does not support.)
--(Next, normal anytype array, dimOf() always returns 0.)
0
--(Next, an instance of class X++ Array, dimOf() always returns 0.)
0
--(Next, the lastIndex method provides size information about Array instances.)
1
--(Next, normal str array, dimOf() does not accept it, job is halted.)
Error executing code: Illegal operation on this type of array. (C)JobsJobDimOfArrays - line 41
************/
/***********  Pop-up error dialog box
"Internal error number 25 in script."
This error is caused by the code line...
info(int2Str(dimOf(iCounts)));
...before iCounts was assigned at any index.
***********/

fieldId2Name

检索一个字符串,该字符串表示由表 ID 号和字段 ID 编号指定的字段的名称。

str fieldId2Name(int tableid, int fieldid)

参数

参数 Description
tableid 表的 ID 号。 注意: 使用 tableName2Id 函数指定表的 ID。
fieldid 字段的 ID 号。

返回值

字段的名称。

注解

若要返回字段名称的可打印版本,请使用 fieldId2PName 函数。

Example

以下示例将 fn 设置为 Customer (CustGroup) 表中字段 ID 为 7 的字段的名称。

static void fieldId2NameExample(Args _arg)
{
    str fn;
    fn = fieldId2Name(tableName2Id("Customer"),7);
}

fieldId2PName

检索由表 ID 号和字段 ID 编号指定的字段的可打印名称。

str fieldId2PName(int tableid, int fieldid)

参数

参数 Description
tableid 表的 ID 号。 注意: 使用 tableName2Id 函数指定表的 ID。
fieldid 字段的 ID 号。 注意: 使用 fieldName2Id 函数指定字段的 ID。

返回值

字段的名称。

Example

static void fieldId2PNameExample(Args _arg)
{
    str name;
    tableid _tableId;
    fieldid _fieldid;

    _tableId = tableName2Id("Address");
    _fieldId = fieldName2Id(_tableId, "Name");
    name = fieldId2PName(_tableId, _fieldid);
    print name;
}

fieldName2Id

检索由表 ID 号和字段 ID 编号指定的表字段的字段 ID。

int fieldName2Id(int tableid, str fieldname)

参数

参数 Description
tableid 表的 ID 号。 注意: 使用 tableName2Id 函数指定表的 ID。
fieldname 字段的名称。

返回值

由 tableidfieldname 参数指定的字段的 ID。

Example

static void fieldName2IdExample(Args _arg)
{
    int id;

    id = fieldName2Id(tableName2Id("Address"), "Name");
    // Returns 6. Name is the 6th field in the Address table.
    print id;
}

indexId2Name

检索索引的名称。

str indexId2Name(int tableid, int indexid)

参数

参数 Description
tableid 索引所属的表的 ID。
indexid 索引的 ID。

返回值

索引的名称。

Example

static void indexId2NameExample(Args _arg)
{
    str s;
    tableid id;
    indexid idx;

    id  = tableName2Id("Address");
    idx = indexName2Id(id, "AddrIdx");
    s = indexId2Name(id, idx);
    print "The result of calling indexId2Name is " + s;
}

indexName2Id

检索索引的 ID。

int indexName2Id(int tableid, str indexname)

参数

参数 Description
tableid 索引所属的表的 ID。
indexname 索引的名称。

返回值

索引的 ID。

Example

static void indexName2IdExample(Args _arg)
{
    indexid idx;
    tableid id;

    id  = tableName2Id("Address");
    idx = indexName2Id(id, "AddrIdx");
    print "Index ID for index name AddrIdx of table Address is " + int2Str(idx);
}

tableId2Name

检索包含表名称的字符串。

str tableId2Name(int _tableid)

参数

参数 Description
_tableid 表的 ID。

返回值

表的名称。

Example

static void tableId2NameExample(Args _arg)
{
    str s;
    tableid id;

    // Get the ID for table name Address.
    id = tableName2Id("Address");
    print "ID for table name Address is " + int2Str(id);

    // Get the name from the table ID.
    s = tableId2Name(id);
    print "Name for table ID " + int2Str(id) + " is " + s;

    // Get the printable name from the table ID.
    s = tableId2PName(id);
    print "Printable name for table ID " + int2Str(id) + " is " + s;
}

tableId2PName

检索包含表的可打印名称(标签)的字符串。

str tableId2PName(int _fieldid)

参数

参数 Description
_fieldid 表的 ID。

返回值

表的标签。

Example

static void tableId2NameExample(Args _arg)
{
    str s;
    tableid id;

    // Get the ID for table name Address.
    id = tableName2Id("Address");
    print "ID for table name Address is " + int2Str(id);

    // Get the name from the table ID.
    s = tableId2Name(id);
    print "Name for table ID " + int2Str(id) + " is " + s;

    // Get the printable name from the table ID.
    s = tableId2PName(id);
    print "Printable name for table ID " + int2Str(id) + " is " + s;
}

tableName2Id

检索表的 ID。

int tableName2Id(str _name)

参数

参数 Description
_名字 表的名称。

返回值

表的 ID。

Example

static void tableName2IdExample(Args _arg)
{
    str s;
    tableid id;

    // Get the ID for the Address table name.
    id = tableName2Id("Address");
    print "ID for the Address table name is " + int2Str(id);

    // Get the name from the table ID.
    s = tableId2Name(id);
    print "Name for table ID " + int2Str(id) + " is " + s;

    // Get the printable name from the table ID.
    s = tableId2PName(id);
    print "Printable name for table ID " + int2Str(id) + " is " + s;
}

typeOf

检索元素的类型。

enum typeOf(anytype _object)

参数

参数 Description
_对象 要为其返回类型的元素。

返回值

类型系统枚举值。

Example

以下示例测试容器 c 中的第一个元素是否是另一个包含单个整数的容器。

if(typeof(conpeek(c, 1)) != Types::Container ||
conlen(conpeek(c, 1)) != 1 ||
typeof(conpeek(conpeek(c, 1), 1)) != Types::Integer)
{
    // More code.
}