CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2000
    Posts
    1

    Accessing Private Fields

    I like to write a library for serializing an object into XML. For this I need to access (read & write)
    private variables of a subclass (the objects has to be derived from my base class). Is there a possibility
    to achieve this with reflection? Or is there an other solution for my problem?

    Thanks a lot for answering my question!


  2. #2
    Join Date
    Dec 1999
    Location
    Chonghe, Taipei County, Taiwan, R.O.C.
    Posts
    231

    Re: Accessing Private Fields

    You may consider the method getDeclaredFields() in the class java.lang.Class. It return the fields
    in the specified class. I adopt some words in the document :
    Returns an array of Field objects reflecting all the fields declared by the class or interface
    represented by this Class object. This includes public, protected, default (package) access,
    and private fields, but excludes inherited fields.
    So I guess that you might have to cast the base class to the subclass and get the field in the subclass.
    I never try it. This is just the suggestion.
    good luck,
    Alfred Wu


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured