|
| 1 | +# Selecting types and members |
| 2 | + |
| 3 | +Every expectation starts from a **selection**: the assemblies, types or members the rule applies to. |
| 4 | +This page covers the two entry points (`In` and `Types`) and navigating between assemblies, types and |
| 5 | +members. |
| 6 | + |
| 7 | +## Sources: the `In` helper |
| 8 | + |
| 9 | +`In` builds the collection of reflection objects you want to reason about. Every source returns a lazily |
| 10 | +evaluated collection that you can navigate and filter further. |
| 11 | + |
| 12 | +| Source | Returns | |
| 13 | +|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| |
| 14 | +| `In.AllLoadedAssemblies()` | all currently loaded assemblies (system assemblies [excluded](./04-configuration.md#assembly-exclusions)) | |
| 15 | +| `In.Assemblies(a1, a2, …)` / `In.Assemblies(collection)` | the given assemblies | |
| 16 | +| `In.AssemblyContaining<T>()` / `In.AssemblyContaining(typeof(T))` | the assembly that declares `T` | |
| 17 | +| `In.Type<T>()` / `In.Type(typeof(T))` | a single type | |
| 18 | +| `In.Types<T1, T2>()` / `In.Types<T1, T2, T3>()` / `In.Types(t1, t2, …)` | the given types | |
| 19 | +| `In.Constructors(…)` / `In.Events(…)` / `In.Fields(…)` / `In.Methods(…)` / `In.Properties(…)` | the given members | |
| 20 | + |
| 21 | +## Sources: the `Types` helper |
| 22 | + |
| 23 | +While `In` starts from concrete reflection objects, `Types` selects types *by criteria*, the natural entry |
| 24 | +point for architecture rules: |
| 25 | + |
| 26 | +| Source | Returns | |
| 27 | +|----------------------------------------------------|--------------------------------------------------------------------------------| |
| 28 | +| `Types.InNamespace("ns")` | all types within a namespace and its sub-namespaces (across loaded assemblies) | |
| 29 | +| `Types.InAllLoadedAssemblies()` | all types in all currently loaded assemblies | |
| 30 | +| `Types.InAssemblies(a1, a2, …)` | all types in the given assemblies | |
| 31 | +| `Types.InAssemblyContaining<T>()` / `…(typeof(T))` | all types in the assembly that declares `T` | |
| 32 | + |
| 33 | +`Types.InNamespace(…)` searches all loaded assemblies by default; chain one of the same `In*` methods directly |
| 34 | +after it to clarify the assembly source (it can only be specified once, before any further filters): |
| 35 | + |
| 36 | +```csharp |
| 37 | +// Defaults to all loaded assemblies |
| 38 | +await Expect.That(Types.InNamespace("MyApp.Domain")).AreSealed(); |
| 39 | + |
| 40 | +// Clarify the assembly source |
| 41 | +await Expect.That(Types.InNamespace("MyApp.Domain").InAssemblyContaining<MyApp.Startup>()) |
| 42 | + .AreSealed(); |
| 43 | +``` |
| 44 | + |
| 45 | +## Navigating to members |
| 46 | + |
| 47 | +From a collection of assemblies or types you can navigate to the members they contain, and from members |
| 48 | +back to their declaring types. |
| 49 | + |
| 50 | +| From | Navigate with | Yields | |
| 51 | +|--------------------|----------------------------------------------------------------------------|--------------------------| |
| 52 | +| assemblies | `.Types()` | the contained types | |
| 53 | +| assemblies / types | `.Methods()`, `.Properties()`, `.Fields()`, `.Events()`, `.Constructors()` | the contained members | |
| 54 | +| members | `.DeclaringTypes()` | the declaring types | |
| 55 | +| types | `.Assemblies()` | the declaring assemblies | |
| 56 | + |
| 57 | +Assemblies also expose shorthand terminals that select a type *kind* directly: |
| 58 | +`.Classes()`, `.Interfaces()`, `.Enums()`, `.Structs()`, `.Records()`, `.RecordStructs()`. |
| 59 | + |
| 60 | +```csharp |
| 61 | +// All types in all (non-system) assemblies |
| 62 | +In.AllLoadedAssemblies().Types() |
| 63 | + |
| 64 | +// Navigate from members back to their declaring types |
| 65 | +In.AllLoadedAssemblies().Methods().DeclaringTypes() |
| 66 | + |
| 67 | +// Shorthand: all public classes |
| 68 | +In.AllLoadedAssemblies().Public.Classes() |
| 69 | +``` |
0 commit comments