Run static method WITHOUT parameters through X++ code (DictClass)


04/12/2020- duocnt    701 Views    

GIỚI THIỆU

Sử dụng DictClass để gọi 1 phương thức static của 1 class bất kỳ nào đó.

Giả sử ta có 1 class ClassContentStaticMethod  với phương thức static methodWithoutParameters như sau:

class ClassContentStaticMethod
{
    static str methodWithoutParameters()
    {
        return "I am'methodWithoutParameters' method in 'ClassContentStaticMethod' class";
    }
}

Để gọi phương thức methodWithoutParameters  từ 1 class khác sử dụng DictClass như sau:


CODE X++

class CallerClass
{
    public static void main(Args _args)
    {
        DictClass           _dictClass;
        ClassName           _className;
        MethodName          _methodName;
        anytype             _result;
        ExecutePermission   _executePermission;
 
            
        _className  = "ClassContentStaticMethod";
        _methodName = "methodWithoutParameters";
 
        // This code will run the static method
        _executePermission = new ExecutePermission();
        _executePermission.assert();
 
        _dictClass = new DictClass(className2Id(_className));
        _result = _dictClass.callStatic(_methodName);
        info( strfmt("%1",_result));
 
        CodeAccessPermission::revertAssert();
    }
}


Kết quả

Góp ý kiến

;
;