Accessing Private Fields from Outside

Jacob Carlborg doob at me.com
Wed May 18 04:54:42 PDT 2011


On 2011-05-18 12:32, gölgeliyele wrote:
> On 5/18/11 5:30 AM, Jacob Carlborg wrote:
>>
>> You can have a look at my serialization library, Orange:
>> http://www.dsource.org/projects/orange
>>
>> I don't think it works with the latest compilers but you have have a
>> look at the source:
>> http://www.dsource.org/projects/orange/browser/orange/util/Reflection.d#L277
>>
>
> This is interesting. The list of limitations seem quite minimal. You
> mention:
>
> 'Due to several bugs in the compiler even the D2 version requires you to
> register a serializer when serializing through base class references'
>
> Is there a list of these bugs? Would resolving these bugs remove the
> need for registration functions?

Don't know if they're actually bugs, more limitations. You would need 
more reflection capabilities. Better support for getting and setting 
instance variables via reflection.

Using ClassInfo.getMembers you can get the members of a class, but you 
can get or set the values. In addition to that there are also two bugs 
related to getMembers:

* http://d.puremagic.com/issues/show_bug.cgi?id=2844
* And one I never can find, getMembers always returns an empty array

If I understand ClassInfo.getMembers correctly, and when it's 
implementation is correct, you should be able to get the members of a 
subclass trough a base class reference.

> Would it be possible to have a library like this in Phobos? Are there
> any performance gotchas?

It would be possible, yes. The current implementation is not the best, 
I'm in the middle of a complete rewrite. The current implementation adds 
instance variables to classes/structs but these should be static.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list