The only way I could get this to work is the function 'DDIF_FIELDINFO_GET'. It receives the name of a dictionary structure, table or type, and returns a list of its fields, and a lot of useful details about them, such as the field's data element, description, length and so on. Here is a basic example:
DATA: lt_fields_info TYPE dfies_tab.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'MARA'
TABLES
dfies_tab = lt_fields_info[]
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Handle errors.
ENDIF.
LOOP AT lt_fields_info[] INTO ls_field_info.
" Dynamically printing the fields' details:
WRITE: / 'Field name: ',
ls_field_info-fieldname,
'Field data element: ',
ls_field_info-rollname,
'Field description: ',
ls_field_info-fieldtext.
ENDLOOP.
Leelaprasad Kolapalli (sorry, I can't find out how to tag the username in my comment) suggested using the function 'DD_GET_FIELD_INFO'. Unfortunately, it didn't work for some DDIC tables, for no apparent reason. This prompted me to search Google for a similar function, and then I found the better one.
Sadly, both functions don't work with local (internal) structures, as defined in classes or includes, so I don't know how to get fields' details for them.
I couldn't get any of those CL_ABAP______DESCR
classes/methods to work, because they either caused a conversion error or just didn't tell me the field's name at all. They did tell me the field's value and basic type, which are NOT what the OP and me are trying to get.
ASSIGN COMPONENT
and all its variations are not helpful either. I can't do ASSIGN COMPONENT 'MANDT' OF STRUCTURE ...
, because I don't know the name of the field! In my specific case at work, I'm using the field's position (index) in the structure, and the command is ASSIGN COMPONENT sy-index OF STRUCTURE ...
.
I've researched the web and found about ten different posts with a whole lot of misleading answers and people who didn't really read the questions or understand them, and I tried all of them without luck, until I found the above function. I hope it is useful for anybody as it was useful to me.