How to find all occurrences of a custom attribute in assemblies?

How can I find every occurrence of a custom attribute inside an assembly?

If you can find all types from the assembly where the attribute is used, but this is not enough. What about methods, properties, enumerations, enum values, fields, etc.

Is there a shortcut to this or the only way to do this is to write code to search for all parts of the type (properties, fields, methods, etc.)?

The reflector does this, not sure how it is implemented.

+3
source share
2 answers

Do

assembly.GetTypes()
    .SelectMany(type => type.GetMembers())
    .Union(assembly.GetTypes())
    .Where(type => Attribute.IsDefined(type, attributeType));

enum, . , , BindingFlags, .

+11

Type.GetMembers(), (, , ..) , . .

, (, , , ), , .

+1

Source: https://habr.com/ru/post/1760300/


All Articles