AlreadyLost
AlreadyLost

Reputation: 827

how to get all static members of a class in C#?

How can I get all static members of a class in c#? I know I can access one like this class1.member1 but I am looking for a way to get all members. Thanks

Upvotes: 2

Views: 1274

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

var members = typeof(class1).GetMembers(BindingFlags.Static | BindingFlags.Public);

(feel free to add NonPublic if you want...)

If you want to get the value of a member, you need to know the member type - either via .MemberType, or by checking the concrete type (via is, etc). Properties are PropertyInfo, for example, and have a GetValue() method that you can pass a null to as the target (obj) for a static property. Fields (FieldInfo) work similarly.

Upvotes: 12

Related Questions