简体   繁体   English

如何在C#中获取类的所有静态成员?

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

How can I get all static members of a class in c#? 如何在C#中获取类的所有静态成员? I know I can access one like this class1.member1 but I am looking for a way to get all members. 我知道我可以访问这样的class1.member1,但我正在寻找一种获取所有成员的方法。 Thanks 谢谢

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

(feel free to add NonPublic if you want...) (如果需要,可以随意添加NonPublic ...)

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). 如果要获取成员的值 ,则需要知道成员类型-通过.MemberType ,或通过检查具体类型(通过is等)。 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. 例如,属性是PropertyInfo ,并具有GetValue()方法,您可以将null传递为静态属性的目标( obj )。 Fields ( FieldInfo ) work similarly. 字段( FieldInfo )的工作类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM