c# - How get all public static members without getters and setters via reflection? -
my code
var memberlist = member in type.getmembers(bindingflags.static | bindingflags.public) .where(m => !(m.name.contains("get_") || m.name.contains("set_") || m.name.contains("add_") || m.name.contains("remove_")) ) select member;
what more elegant way?
upd: , without add_/remove_ members of event handlers
you can make query of defined accessors , exclude those:
var propertyaccessors = type.getproperties(bindingflags.static | bindingflags.public) .selectmany(p => p.getaccessors()) .cast<memberinfo>(); var eventaccessors = type.getevents(bindingflags.static | bindingflags.public) .selectmany(e => new[] { e.getaddmethod(true), e.getremovemethod(true) }) .cast<memberinfo>(); var accessors = propertyaccessors.concat(eventaccessors); var memberlist = type.getmembers(bindingflags.static | bindingflags.public) .except(accessors);
you might able away filtering out methods don't have special names (as defined methodbase.isspecialname
.
var memberlist = type.getmembers(bindingflags.static | bindingflags.public) .where(m => { var method = m methodbase; return method == null || !method.isspecialname; });
Comments
Post a Comment