X++ 会话运行时函数

注释

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

本文介绍会话运行时函数。

curExt

检索用于当前公司的扩展。

str curExt()

返回值

当前公司的扩展。

Example

static void curExtExample(Args _arg)
{
    str s;
    // Sets s to the extension of the current company.
    s = curExt();
    print "Current extension is " + s;
}

curUserId

检索表示当前用户的非数值 ID。

str curUserId()

返回值

表示当前用户的非数值 ID。

Example

static void curUserIdExample(Args _arg)
{
    str s;
    s = curUserId();
    print "Current user ID is " + s;
}

funcName

检索包含当前函数上下文的字符串。

str funcName()

返回值

正在执行此方法的方法的名称。

注解

如果执行当前位于表或类的成员内,则方法的名称以该表或类的名称为前缀。

Example

static void funcNameExample(Args _arg)
{
    print "Current function context is " + funcName();
}

getCurrentPartition

检索当前分区的短名称。

str getCurrentPartition()

返回值

当前分区的短名称。

注解

返回的数据分区名称的最大长度为 8 个字符。

Example

下面的代码示例演示了对 X++ 语言的 getCurrentPartition 函数以及相关函数或方法的调用和输出。

static public void Main(Args _args)  // X++ method.
{
    int64 iPartition;
    str sPartition;
    SelectableDataArea oSelectableDataArea;  // System ExDT.
    iPartition = getCurrentPartitionRecId();
    sPartition = getcurrentpartition();
    oSelectableDataArea = Global::getCompany( tableNum(BankAccountTable) );
    Global::info( strFmt(
            "getCurrentPartitionRecId =%1 , getCurrentPartition =%2 , getCompany =%3",
            iPartition, sPartition, oSelectableDataArea) );
}
/**** Pasted from Infolog window:
Message_@SYS14327 (03:42:38 pm)
getCurrentPartitionRecId =5637144576 , getCurrentPartition =initial , getCompany =ceu
****/

getCurrentPartitionRecId

检索当前分区的 RecId 字段。

int64 getCurrentPartitionRecId()

返回值

当前数据分区的 RecId 字段。

注解

若要查看依赖于 getCurrentPartitionRecId 函数的代码示例,请参阅 How to: Include a Filter for Partition in Direct Transact-SQL

Example

下面的代码示例演示了对 X++ 语言的 getCurrentPartitionRecId 函数以及相关函数或方法的调用和输出。

static public void Main(Args _args)  // X++ method.
{
    int64 iPartition;
    str sPartition;
    SelectableDataArea oSelectableDataArea;  // System ExDT.
    iPartition = getCurrentPartitionRecId();
    sPartition = getcurrentpartition();
    oSelectableDataArea = Global::getCompany( tableNum(BankAccountTable) );
    Global::info( strFmt(
            "getCurrentPartitionRecId =%1 , getCurrentPartition =%2 , getCompany =%3",
            iPartition, sPartition, oSelectableDataArea) );
}
/**** Pasted from Infolog window:
Message_@SYS14327 (03:42:38 pm)
getCurrentPartitionRecId =5637144576 , getCurrentPartition =initial , getCompany =ceu
****/

getPrefix

在连续调用 setPrefix 函数后检索当前执行前缀。

str getPrefix()

返回值

当前执行前缀。

注解

通过前缀机制,可以更直接地编写有关应用程序执行的事务的精确错误消息。 由于在 Infolog 中创建分层显示,因此可以更轻松地确定每个错误来自何处。

Example

static void getPrefixExample(Args _arg)
{
    setPrefix("Prefix");
    setPrefix("Another prefix");
    print getPrefix();
}

sessionId

检索当前会话的会话编号。

int sessionId()

返回值

当前会话的数值 ID。

注解

启动客户端并连接到应用程序对象服务器(AOS)时分配会话编号。 客户端生存期内此函数的每个调用都会返回相同的整数值。 返回的值与 SessionID 扩展数据类型兼容。 包含的方法返回有关单个用户会话的信息。

Example

static void sessionIdExample(Args _arg)
{
    int session;
    session = sessionId();
    print "This session ID is number " + int2Str(session);
}

prmIsDefault

确定当前方法的指定参数是否具有默认值。

int prmIsDefault(anytype argument)

参数

参数 Description
论点 要测试的参数。

返回值

如果使用参数的默认值,则为 1;否则为 0(零)。

Example

static void prmIsDefaultExample(Args _arg)
{
    void fn(boolean b = true, int j = 42)
    {
        if (prmIsDefault(b) == 1)
        {
            print "First parameter is using the default value.";
        }
        else
        {
            print "First parameter is not using the default value.";
        }
    }
    fn();
    fn(false);
}

runAs

使调用方能够在其他用户的安全上下文中运行 X++ 方法。 此函数最常用于批处理。

container runAs(
    str userId,
    int classId,
    str staticMethodName
    [,
    container params,
    str company,
    str language,
    str partition
    ])

参数

参数 Description
userId 要模拟的用户。
classId 在模拟会话中调用的类。
staticMethodName 在新用户上下文中调用的类方法。
参数 要传递给方法的参数;自选。
company 为模拟会话选择的公司;自选。
语言 为模拟会话选择的语言;自选。
分区 getCurrentPartition 函数返回的类型分区键;自选。

返回值

包含 runAs 函数调用的方法的返回值或值的容器(如果返回任何值)。

注解

此函数允许以其他用户身份运行代码。 此功能存在安全威胁。 因此,此函数在 代码访问安全性下运行。 对服务器上的此函数的调用需要 来自 RunAsPermission 类的权限。 此应用程序编程接口(API)的每个用法都应进行威胁建模。 如果发现安全漏洞,请验证此 API 的输入。 调试器可能会忽略位于使用 runAs 函数调用的方法中的断点。 由 runAs 函数执行的 X++ 代码必须作为 Microsoft .NET Framework 公共中间语言 (CIL) 运行。 如果尚未为目标静态方法生成 CIL,则错误消息指示找不到该方法。 PartitionKey 系统类型是分区参数的确切类型。 PartitionKey 是最大长度为 8 个字符的字符串。

Example

以下示例在 EventJobDueDateDate 类中调用 runDueDateEventsForUser 方法。 代码在用户的安全上下文中运行。 通过将此代码应用到新类中的方法来运行此代码。

server static public void Main(Args _args)
{
    RunAsPermission perm;
    UserId runAsUser;
    SysUserInfo userInfo;
    userInfo = SysUserInfo::find();
    runAsUser = userInfo.Id;
    perm = new RunAsPermission(runAsUser);
    perm.assert();
    runAs(runAsUser, classnum(EventJobDueDate), "runDueDateEventsForUser");
    CodeAccessPermission::revertAssert();
}

setPrefix

设置当前执行范围的前缀。

int setPrefix(str _prefix)

参数

参数 Description
_前缀 当前执行范围的前缀。

返回值

如果已成功设置前缀,则为 0

注解

可以使用 getPrefix 函数提取执行的完整前缀。 当范围保留时,前缀会自动重置为上一级别。 通过前缀机制,可以更直接地编写有关应用程序执行的事务的精确错误消息。 例如, AA 方法调用 BB 方法,每个方法都调用 setPrefix 函数。 BB 方法写入 Infolog 的消息显示在层次结构中嵌套。 当 BB 方法结束和控制返回到 AA 方法时,BB 方法设置的前缀不会附加到后续消息。

Example

static void setPrefixExample(Args _arg)
{
    int i;
    i = setPrefix("Prefix");
    print i;
}