Показать сообщение отдельно
Старый 04.02.2013, 16:23   #1  
plumbum is offline
plumbum
Участник
Соотечественники
 
182 / 86 (3) ++++
Регистрация: 07.12.2007
Адрес: Vienna, AT
DataMembers для DataContract автоматически
Вот набросал простой скрипт, создающий методы аннотированные [DataMemberAttribute] для всех переменных класса:

X++:
static void GenerateDataMembers(Args _args)
{
    #define.classDeclaration('classDeclaration')
    str strAttributeMethodLine1 = '[DataMemberAttribute]\n';
    str strAttributeMethodLine2 = 'public %1 parm%2(%1 _%2 = %2)\n';
    str strAttributeMethodLine3 = '{\n';
    str strAttributeMethodLine4 = '\t%2 = _%2;\n';
    str strAttributeMethodline5 = '\treturn %2;\n';
    str strAttributeMethodLine6 = '}';
    
    str strAttributeMethod      = strAttributeMethodLine1 +
                                  strAttributeMethodLine2 +
                                  strAttributeMethodLine3 +
                                  strAttributeMethodLine4 +
                                  strAttributeMethodLine5 +
                                  strAttributeMethodLine6;        
    str                         lstrAttributeMethodSource;
        
    ClassId                     lclsClassId             = classNum(<MyClassName>); //change this paramter
    
    SysDictClass                lclsDictclass           = new SysDictClass(lclsClassId);
    ClassBuild                  lclsClassBuild          = new ClassBuild(lclsDictclass.name(), true);
    
    Set                         lsetMethods             = lclsDictClass.methods(true, false, false);
    SetEnumerator               lsenMethods             = lsetMethods.getEnumerator();
    Map                         lmapMethods             = new Map(Types::String, Types::Class);
    MapEnumerator               lmenMethods;
    SysDictMethod               lclsMethod;
    str                         lstrClassDeclaration    = lclsDictclass 
                                                          ? TreeNode::findNode(strFmt('\\Classes\\%1\\classDeclaration', lclsDictclass.name())).AOTgetSource()
                                                          : '';
    
    Map                         lmapMemberVariableParm  = new Map(Types::String, Types::String);
    
    str                         lstrMemberVarPare;
    str                         lstrMemberVarType;    
    str                         lstrMemberVarName;
    str                         lstrMemberVarMName;
    str                         lstrMemberVarPrefName;
    
    int                         lintStrLen, 
                                lintMemberVarStartPos, 
                                lintMemberVarEndPos;
    container                   lconMemberVars;
    
    int                         lintConLen, i;
    
    
    ;    
    
    if (!lstrClassDeclaration) 
        return;
    
    while (lsenMethods.moveNext())
    {
        lclsMethod = lsenMethods.current();
        lmapMethods.insert(lclsMethod.name(), lclsMethod);
    }
    
    lintStrLen                  = strLen(lstrClassDeclaration);
    lintMemberVarStartPos       = strFind(lstrClassDeclaration, '{' ,1, lintStrLen);
    lintMemberVarEndPos         = strFind(lstrClassDeclaration, '}',1, lintStrLen);
 
    lstrClassDeclaration        = subStr(
                                    lstrClassDeclaration, 
                                    lintMemberVarStartPos+1, 
                                    lintMemberVarEndPos - lintMemberVarStartPos - 1);
    
    lstrClassDeclaration        = strReplace(lstrClassDeclaration, '\n', '');
    lstrClassDeclaration        = strReplace(lstrClassDeclaration, '\t', '');
    lstrClassDeclaration        = strReplace(lstrClassDeclaration, ';', ',');
    lstrClassDeclaration        = subStr(lstrClassDeclaration, 1, strLen(lstrClassDeclaration) - 1);
    
    lconMemberVars              = str2con(lstrClassDeclaration);
    lintConLen                  = conLen(lconMemberVars);
    
    for (i = 1; i <= lintConLen; i++)
    {
        lstrMemberVarPare       = conPeek(lconMemberVars, i);
        lstrMemberVarPare       = strLRTrim(lstrMemberVarPare);
        lstrMemberVarType       = subStr(lstrMemberVarPare, 1, strFind(lstrMemberVarPare, ' ', 1, strLen(lstrMemberVarPare))-1);
        lstrMemberVarName      = strLTrim(subStr(lstrMemberVarPare, strLen(lstrMemberVarType) +1 , strLen(lstrMemberVarPare) - strLen(lstrMemberVarType)));                
        
        if (! lmapMethods.exists('parm'+lstrMemberVarName))
        {
            lstrAttributeMethodSource = strFmt(strAttributeMethod, lstrMemberVarType, lstrMemberVarName);
            lclsClassBuild.addMethod('parm' + lstrMemberVarName, lstrAttributeMethodSource);            
        }
    }
    lclsClassBuild.classNode().AOTcompile();
    
}
уже существующие методы пересоздаваться не будут.

P.S.
а то уже совсем надоело все это "руками" делать.
Надеюсь еще кому-то пригодиться
__________________
http://www.axdevposts.blogspot.com
Пришел, уведел.... отойди, дай другому увидеть!
За это сообщение автора поблагодарили: mazzy (2), trud (1), Logger (1), gl00mie (2).