Compartilhar via


Funções de reflexão em tempo de execução da X++

Observação

Grupos de interesse da comunidade mudaram do Yammer para o Microsoft Viva Engage. Para ingressar em uma comunidade do Viva Engage e participar das discussões mais recentes, preencha o formulário Solicitar acesso às Finanças e Operações viva engage community e escolha a comunidade que você deseja ingressar.

Este artigo descreve as funções de tempo de execução de reflexão.

classIdGet

Recupera o identificador numérico (a ID da classe) da classe à qual o objeto inicializado pertence.

int classIdGet(class object)

Parâmetros

Parâmetro Description
objeto O objeto para o qual obter a ID da classe.

Valor de retorno

A ID da classe do objeto especificado.

Example

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

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

dimOf

Recupera o número de elementos de índice para os quais o espaço foi alocado em uma matriz X++.

int dimOf(anytype object)

Parâmetros

Parâmetro Description
objeto A matriz da qual determinar o tamanho da dimensão.

Valor de retorno

Se o valor do parâmetro de objeto for uma matriz, o número de elementos na matriz; caso contrário, 0 (zero).

Observações

A função dimOf destina-se a matrizes X++ declaradas como os seguintes tipos primitivos X++:

  • boolean
  • date
  • int
  • int64
  • real
  • utcDateTime

Um exemplo é int iAmounts[6];. Matrizes de valores de enumeração e tipos de dados estendidos também têm suporte se, em última análise, forem baseadas em um dos tipos de dados primitivos anteriores (como int). A função dimOf não aceita matrizes de todos os tipos primitivos X++. Aqui estão os tipos de matriz que a função dimOf não aceita:

  • str
  • contêiner
  • anytype
  • Matrizes de objetos de classe
  • Instâncias da classe 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

Recupera uma cadeia de caracteres que representa o nome do campo especificado por um número de ID da tabela e um número de ID de campo.

str fieldId2Name(int tableid, int fieldid)

Parâmetros

Parâmetro Description
tableid O número de ID da tabela. Nota: Use a função tableName2Id para especificar a ID de uma tabela.
fieldid O número de ID do campo.

Valor de retorno

O nome do campo.

Observações

Para retornar uma versão imprimível do nome do campo, use a função fieldId2PName .

Example

O exemplo a seguir define fn como o nome do campo na tabela Cliente (CustGroup) que tem uma ID de campo de 7.

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

fieldId2PName

Recupera o nome imprimível do campo especificado por um número de ID da tabela e um número de ID de campo.

str fieldId2PName(int tableid, int fieldid)

Parâmetros

Parâmetro Description
tableid O número de ID da tabela. Nota: Use a função tableName2Id para especificar a ID de uma tabela.
fieldid O número de ID do campo. Nota: Use a função fieldName2Id para especificar a ID de um campo.

Valor de retorno

O nome do campo.

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

Recupera a ID do campo de campo da tabela especificada por um número de ID da tabela e um número de ID de campo.

int fieldName2Id(int tableid, str fieldname)

Parâmetros

Parâmetro Description
tableid O número de ID da tabela. Nota: Use a função tableName2Id para especificar a ID de uma tabela.
nome do campo O nome do campo.

Valor de retorno

A ID do campo especificado pelos parâmetros tableid e fieldname .

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

Recupera o nome de um índice.

str indexId2Name(int tableid, int indexid)

Parâmetros

Parâmetro Description
tableid A ID da tabela à qual o índice pertence.
indexid A ID do índice.

Valor de retorno

O nome do índice.

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

Recupera a ID de um índice.

int indexName2Id(int tableid, str indexname)

Parâmetros

Parâmetro Description
tableid A ID da tabela à qual o índice pertence.
indexname O nome do índice.

Valor de retorno

A ID do índice.

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

Recupera uma cadeia de caracteres que contém o nome de uma tabela.

str tableId2Name(int _tableid)

Parâmetros

Parâmetro Description
_tableid A ID da tabela.

Valor de retorno

O nome da tabela.

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

Recupera uma cadeia de caracteres que contém o nome imprimível (o rótulo) de uma tabela.

str tableId2PName(int _fieldid)

Parâmetros

Parâmetro Description
_fieldid A ID da tabela.

Valor de retorno

O rótulo da tabela.

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

Recupera a ID de uma tabela.

int tableName2Id(str _name)

Parâmetros

Parâmetro Description
_nome O nome da tabela.

Valor de retorno

A ID da tabela.

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

Recupera o tipo de um elemento.

enum typeOf(anytype _object)

Parâmetros

Parâmetro Description
_objeto O elemento para o qual retornar o tipo.

Valor de retorno

Um valor de enumeração do sistema Types .

Example

O exemplo a seguir testa se o primeiro elemento em um contêiner, c, é outro contêiner que contém um único inteiro.

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