Reputation: 7197
I have a class with static members. I want to get a list of all the static members that their class is defined in the namespace "foo" and inherit from class "bar"
something like list x= typeof(this).staticmembers.where(namespace == "foo");
TRY1:
var AllowedTypes = GetType().Assembly.GetTypes()
.Where(x => x.Namespace == "XX.XXX.XXX")
.Where(x => x.IsAssignableFrom(typeof(UserControl)));
var StaticMembersOfAllowedTypes = typeof(MainWindowXX).GetMembers (System.Reflection.BindingFlags.Static ).Where(item => AllowedTypes.Contains(item));
this gives me a list of member infos, but I want the items themselves.
so I can do foreach item compare to another item.
Upvotes: 3
Views: 515
Reputation: 8352
I think this will do the trick. It's possible there's some typos, I couldn't test it
var list x = GetType().Assembly.GetTypes()
.Where(x => x.Namespace == "foo" && typeof(bar).IsAssignableFrom(x))
.SelectMany(x => x.GetMembers(BindingFlags.Static));
You may need to add BindingFlags.Public
:
x.GetMembers(BindingFlags.Static | BindingFlags.Public)
Upvotes: 4