__traits, compile time member type info

Lloyd Dupont ld-REMOVE at galador.net
Thu Jun 16 05:34:21 PDT 2011


I'm trying to build an introspection system for a project I have.
I already have a working template to get members value
=== working members getter ===
Variant GETTER(T, string member)(Object target)
{
    T tt = cast(T)target;
    if (!tt)
        throw new ReflectionException("target is null or not " ~T.stringof 
~" value: " ~target.toString());
    return Variant(__traits(getMember, tt, member));
}
===========================

Now I'm trying to implement a working setter. I have a problem for testing 
and converts the value.
Ideally I'd like something like the code below
===== not compiling setter ====
void SETTER(T, string member)(Object target, Variant value)
{
    T tt = cast(T)target;
    if (!tt)
        throw new ReflectionException("target is null or not " ~T.stringof 
~" value: " ~target.toString());

    if(!value.convertsTo!( typeid(__traits(getMember, T, member))) )
        throw new ReflectionException("Can't convert " ~value.stringof ~" to 
" ~typeid(__traits(getMember, T, member)).toString());
    __traits(getMember, tt, member) = value.coerce( 
typeid(__traits(getMember, T, member)) );
}
==========================

it doesn't compile because I don't know how to get the type, at compile 
time, of member "member" of type "T"
how do I do that please? 



More information about the Digitalmars-d-learn mailing list