如何使用反射从classDeclaration中检索属性

时间:2014-03-25 19:30:34

标签: reflection axapta x++ dynamics-ax-2012

我想创建一个将应用于classDeclarations的自定义属性。我可以枚举类中其他方法的属性,但不能枚举classDeclaration本身,因为它是某种特殊方法

我知道这是可能的,因为SysObsoleteAttribute(从内核调用)全部放在classDeclarations中。

Classes\CustCustomerService\create中,我只是将属性复制到顶部的Classes\CustCustomerService\classDeclaration进行此测试。

[AifDocumentCreateAttribute, SysEntryPointAttribute(true)]
class CustCustomerService extends AifDocumentService
{

}

我在新类上创建了一个静态方法:

static public void AttribsOfSysEntryPointAttributeOnMethod
            (
            str _sNameOfClass,
            str _sNameOfMethod,
            str _nameOfAttribute
            )
{
    int nClassId;

    SysDictMethod       sdm;
    Object attributeAsObject;
    SysDictClass            sysDictClass;

    Array                   attribArray = new Array(Types::Class);
    int i;

    nClassId = Global::className2Id(_sNameOfClass);

    sysDictClass    = new SysDictClass(nClassId);

    sdm = new SysDictMethod(UtilElementType::ClassInstanceMethod, nClassId, _sNameOfMethod);

    attribArray = sdm.getAllAttributes();

    if (attribArray)
    {
        for (i=1; i<=attribArray.lastIndex(); i++)
        {
            attributeAsObject = attribArray.value(i);

            info(strFmt("[%3] Attrib Class Id: %1 [%2]", classIdGet(attributeAsObject), classId2Name(classIdGet(attributeAsObject)), _sNameOfMethod));

        }
    }
    else
    {
        // Unable to get attributes, try another way
        error(strFmt("Unable to retrieve attribute array for method %1", sdm.name()));

        // It is, so let's try and enumerate ALL attributes and output them directly from class dec
        sdm = sysDictClass.objectMethodObject(1);

        if (attribArray)
        {
            for (i=1; i<=attribArray.lastIndex(); i++)
            {
                attributeAsObject = attribArray.value(i);

                info(strFmt("[%3] Attrib Class Id: %1 [%2]", classIdGet(attributeAsObject), classId2Name(classIdGet(attributeAsObject)), _sNameOfMethod));

            }
        }
        else
            error(strFmt("Still unable to retrieve attribute array for method %1", sysDictClass.objectMethod(1)));
    }
}

然后创建了一个调用它的作业,你可以看到它对一个方法的作用,而不是另一个方法。

static void Job5(Args _args)
{        
    AttributeReflection::AttribsOfSysEntryPointAttributeOnMethod("CustCustomerService", "create", "SysEntryPointAttribute");
    AttributeReflection::AttribsOfSysEntryPointAttributeOnMethod("CustCustomerService", "classDeclaration", "SysEntryPointAttribute");

}

如何从classDeclaration枚举属性?

的任何想法

1 个答案:

答案 0 :(得分:1)

classDeclaration不是方法,无法调用。因此,您的sysDictClass变量为空。

Googling显示getAllAttributes方法退出DictClass

attribArray = sdm ? sdm.getAllAttributes() : sysDictClass.getAllAttributes();