How can get all non-static members of a type?

2019-07-13 00:50发布

问题:

__traits(allMembers, T) returns both instance and static members. How can I filter out the static members? I'd like this to work for both fields and methods.

回答1:

Sure you can do this. D's introspection power is immense, in your case Filter from std.meta is your friend ;-)

struct Lion
{
    static maxSpeed = 100;
    string name;
    bool isDangerous()
    {
        return true;
    }
    static bool isAlive(uint meat)
    {
        return meat > 100;
    }
}

template FilterMembers(alias T, bool filterStatic = true)
{
    import std.meta : Filter;
    template filter(string member)
    {
        enum memberStr = T.stringof ~ "." ~ member;
        enum isStatic = __traits(isStaticFunction, mixin(memberStr)) ||
                        __traits(compiles, mixin("{auto b = " ~ memberStr ~ ";}"));
        enum filter = filterStatic ^ isStatic;
    }
    enum FilterMembers = Filter!(filter, __traits(allMembers, T));
}


unittest
{
    import std.meta : AliasSeq;
    assert(FilterMembers!Lion == AliasSeq!("name", "isDangerous"));
    assert(FilterMembers!(Lion, false) == AliasSeq!("maxSpeed", "isAlive"));
}

// or using the old-school main
version(unittest) {} else
void main()
{
    import std.stdio;
    foreach (member; FilterMembers!Lion)
        writeln(member);
}


标签: reflection d