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

天涯浪子 提交于 2020-01-15 05:01:09

问题


__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);
}


来源:https://stackoverflow.com/questions/38813868/how-can-get-all-non-static-members-of-a-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!