Mar*_*cus 3 c# reflection custom-attributes
如何在程序集中找到每个自定义属性?
如果可以从使用该属性的程序集中找到所有类型,但这还不够.方法,属性,枚举,枚举值,字段等如何
这样做有什么快捷方式,或者是编写代码来搜索某个类型的所有部分(属性,字段,方法等)的唯一方法吗?
Reflector做到了这一点,不知道它是如何实现的.
Kir*_*oll 11
做,
assembly.GetTypes()
.SelectMany(type => type.GetMembers())
.Union(assembly.GetTypes())
.Where(type => Attribute.IsDefined(type, attributeType));
Run Code Online (Sandbox Code Playgroud)
这也将返回enum
值,因为它们只是引擎盖下的公共静态字段.此外,如果你想要私人会员,你必须调整BindingFlags
你的传递.