smarter reflection issue

Jesse Phillips jessekphillips+D at gmail.com
Thu Jun 16 13:59:48 PDT 2011


MemberDesc MEMBER(T, string memberName)() if(std.traits.hasMember!(T, memberName))
{
...
}

Lloyd Dupont Wrote:

> I have a MemberDesc class which describe a class's members.
> I fill it with a template method like that (with GETTER and SETTER some 
> other templated method I wrote)
> =====
> MemberDesc MEMBER(T, string memberName)()
> {
>     TypeInfo ti = typeid( typeof(__traits(getMember, T, memberName)) );
> 
>     Variant function(Object target) getter = null;
>     getter = &GETTER!(T, memberName);
> 
> 
>     void function(Object target, Variant value) setter = null;
>     setter = &SETTER!(T, memberName);
> 
>     return new MemberDesc(memberName, ti , getter, setter);
> }
> =====
> 
> And it works except that I don't do any check that the setter / getter 
> method exist!
> 
> I tried something like that
> ====
> static if( __traits(compiles, __traits(getMember, T, member)) )
> {
>     getter = &GETTER!(T, memberName);
> }
> ====
> 
> but this always fail... mm.. how could I check for the getter?
> 
> 
> and i guess it's even harder for the setter!
> how about something like that (how to fix it?)
> =====
> static if( __traits(compiles, __traits(getMember, T, member) = 
> typeof(__traits(getMember, T, memberName)).init) )
> {
>     setter = &SETTER!(T, memberName);
> }
> =====
> 



More information about the Digitalmars-d-learn mailing list