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


04/12/2020- duocnt    840 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 methodWithParameters như sau:

class ClassContentStaticMethod

    static str methodWithParameters(str _parm1, str _parm2, str _parm3)
    {
        str _return;
        str _rawStr;
 
        _rawStr = "I am 'methodWithParameters' method in'ClassContentStaticMethod' class with                                         Parameter1 = '%1',                                         Parameter2 = '%2',                                         Parameter3 = '%3'";
        _return = strFmt(_rawStr,_parm1,_parm2,_parm3);
        return _return;
    }
}

Để gọi phương thức methodWithParameters  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;
        str                 _parm1,_parm2,_parm3;
 
        ExecutePermission   _executePermission;
            
        _className  = "ClassContentStaticMethod";
        _methodName = "methodWithParameters";
 
        _parm1  =   "Nguyễn";
        _parm2  =   "Thành";
        _parm3  =   "Được";
 
        // This codewill run the static method
        _executePermission = new ExecutePermission();
        _executePermission.assert();
 
        _dictClass = new DictClass(className2Id(_className));
        _result = _dictClass.callStatic(_methodName,_parm1,_parm2,_parm3);
        info( strfmt("%1",_result));
 
        CodeAccessPermission::revertAssert();
    }
}


Kết quả.




Góp ý kiến

;
;