Selecting types and members
Every expectation starts from a selection: the assemblies, types or members the rule applies to.
This page covers the two entry points (In and Types)
and navigating between assemblies, types and members.
Sources: the In helper
In builds the collection of reflection objects you want to reason about. Every source returns a lazily
evaluated collection that you can navigate and filter further.
| Source | Returns |
|---|---|
In.AllLoadedAssemblies() | all currently loaded assemblies (system assemblies excluded) |
In.Assemblies(a1, a2, …) / In.Assemblies(collection) | the given assemblies |
In.AssemblyContaining<T>() / In.AssemblyContaining(typeof(T)) | the assembly that declares T |
In.Type<T>() / In.Type(typeof(T)) | a single type |
In.Types<T1, T2>() / In.Types<T1, T2, T3>() / In.Types(t1, t2, …) | the given types |
In.Constructors(…) / In.Events(…) / In.Fields(…) / In.Methods(…) / In.Properties(…) | the given members |
When a source is evaluated
A source is read each time an expectation enumerates it, not when it is created: a source stored in a field also sees assemblies loaded later and the assembly exclusions customized at that time, and a collection you pass in is enumerated again on every evaluation.
Sources: the Types helper
While In starts from concrete reflection objects, Types selects types by criteria, the natural entry
point for architecture rules:
| Source | Returns |
|---|---|
Types.InNamespace("ns") | all types within a namespace and its sub-namespaces (across loaded assemblies) |
Types.InAllLoadedAssemblies() | all types in all currently loaded assemblies |
Types.InAssemblies(a1, a2, …) | all types in the given assemblies |
Types.InAssemblyContaining<T>() / …(typeof(T)) | all types in the assembly that declares T |
Types.InNamespace(…) searches all loaded assemblies by default; chain one of the same In* methods directly
after it to clarify the assembly source:
// Defaults to all loaded assemblies
await That(Types.InNamespace("MusicStore.Domain")).AreSealed();
// Clarify the assembly source
await That(Types.InNamespace("MusicStore.Domain").InAssemblyContaining<MusicStore.Startup>())
.AreSealed();
Navigating to members
From a collection of assemblies or types you can navigate to the members they contain, and from members back to their declaring types. From types you can also navigate to related types.
| From | Navigate with | Yields |
|---|---|---|
| assemblies | .Types() | the contained types |
| assemblies / types | .Methods(), .Properties(), .Fields(), .Events(), .Constructors() | the contained members |
| members | .DeclaringTypes() | the declaring types |
| types | .Assemblies() | the declaring assemblies |
| types | .BaseTypes() | the base types, up the inheritance chain |
| types | .ImplementedInterfaces() | the implemented interfaces (.Directly(): declared) |
| types | .NestedTypes() | the types nested directly in them |
A type reached from several types is included once. .BaseTypes() does not include the implicit base types
object, ValueType and Enum. With .ImplementedInterfaces().Directly() an interface that a type receives
through its base class or through another implemented interface is not included.
Assemblies also expose shorthand terminals that select a type kind directly:
.Classes(), .Interfaces(), .Enums(), .Structs(), .Records(), .RecordStructs(), .Delegates(),
.Exceptions(), .Attributes().
// All types in all (non-system) assemblies
In.AllLoadedAssemblies().Types()
// Navigate from members back to their declaring types
In.AllLoadedAssemblies().Methods().DeclaringTypes()
// Shorthand: all public classes
In.AllLoadedAssemblies().Public.Classes()
Navigating to related types describes the source in the failure message, e.g.
interfaces implemented by types within namespace "MusicStore.Domain" in assembly containing type Album:
// Domain types only implement interfaces of the domain or of the framework
await That(Types.InNamespace("MusicStore.Domain").InAssemblyContaining<Album>()
.ImplementedInterfaces()
.WhichAreNotWithinNamespace("System"))
.AreWithinNamespace("MusicStore.Domain");
// Every exception of the library is sealed
await That(In.AssemblyContaining<Album>().Exceptions())
.AreSealed();