有时候我们要在程序运行的时候取得某个内表或者某个结构它的属性或者它的字段的属性,可能通过类CL_ABAP_TYPEDESCR和它的子类取得指定内表的属性。
类CL_ABAP_TYPEDESCR和它的子类的结构图
CL_ABAP_TYPEDESCR
|
|--CL_ABAP_DATADESCR
| |
| |--CL_ABAP_ELEMDESCR
| |--CL_ABAP_REFDESCR
| |--CL_ABAP_COMPLEXDESCR
| |
| |--CL_ABAP_STRUCTDESCR
| |--CL_ABAP_TABLEDESCR
|
|--CL_ABAP_OBJECTDESCR
|
|--CL_ABAP_CLASSDESCR
|--CL_ABAP_INTFDESCR、
例1:
REPORT ZTEST_UPEXCEL. DATA: name(10) TYPE c, *WA_DEPT TYPE TYP_DEPT, DESCR_REF TYPE REF TO CL_ABAP_TYPEDESCR. FIELD-SYMBOLS: <FS_DEPT> TYPE ABAP_COMPDESCR. ************************************************************************ * START-OF-SELECTION ************************************************************************ START-OF-SELECTION. DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_data( name ). BREAK-POINT.
例2:
1 TYPES: 2 my_table TYPE HASHED TABLE OF i WITH UNIQUE KEY TABLE LINE. 3 DATA: 4 descr_ref TYPE ref to cl_abap_tabledescr. 5 FIELD-SYMBOLS: 6 <key_comp_wa> TYPE abap_keydescr. 7 START-OF-SELECTION. 8 descr_ref ?= cl_abap_typedescr=>describe_by_name( 'MY_TABLE' ). 9 WRITE: / 'Typename :', descr_ref->absolute_name. 10 WRITE: / 'Kind :', descr_ref->type_kind. 11 WRITE: / 'Length :', descr_ref->length. 12 WRITE: / 'Decimals :', descr_ref->decimals. 13 WRITE: / 'Table Kind :', descr_ref->table_kind. 14 WRITE: / 'Initial Size :', descr_ref->initial_size. 15 WRITE: / 'Key Def Kind :', descr_ref->key_defkind. 16 WRITE: / 'Has Unique Key:', descr_ref->has_unique_key. 17 WRITE: / 'Key Components:'. 18 LOOP AT descr_ref->key ASSIGNING <key_comp_wa>. 19 WRITE <key_comp_wa>-name. 20 ENDLOOP.
例3:
1 TYPES: 2 3 BEGIN OF TYP_DEPT, 4 5 ID(10) TYPE N, 6 7 NAME(10) TYPE C, 8 9 END OF TYP_DEPT. 10 11 DATA: 12 13 WA_DEPT TYPE TYP_DEPT, 14 15 DESCR_REF TYPE REF TO CL_ABAP_STRUCTDESCR. 16 17 FIELD-SYMBOLS: 18 19 <FS_DEPT> TYPE ABAP_COMPDESCR. 20 21 ************************************************************************ 22 23 * START-OF-SELECTION 24 25 ************************************************************************ 26 27 START-OF-SELECTION. 28 29 DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( WA_DEPT ). 30 31 LOOP AT DESCR_REF->COMPONENTS ASSIGNING <FS_DEPT>. 32 33 WRITE: / <FS_DEPT>-NAME. 34 35 ENDLOOP.