Skip to content

Commit 87b9e88

Browse files
ode2code95khellang
authored andcommitted
Document support for compiled view types
1 parent c2aa734 commit 87b9e88

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ collection.Scan(scan => scan
6666
.AsImplementedInterfaces());
6767
```
6868

69+
#### Scanning compiled view (UI) types
70+
By default, Scrutor excludes compiler-generated types from the `.AddClasses()` type filters. When loading views from a framework such as [Avalonia UI](https://avaloniaui.net/), we need to opt in to compiler-generated types, like this:
71+
72+
```csharp
73+
.AddClasses(classes => classes
74+
// Opt-in to compiler-generated types
75+
.WithAttribute<CompilerGeneratedAttribute>()
76+
// Optionally filter types to reduce number of service registrations.
77+
.InNamespaces("MyApp.Desktop.Views")
78+
.AssignableToAny(
79+
typeof(Window),
80+
typeof(UserControl)
81+
)
82+
.AsSelf()
83+
.WithSingletonLifetime()
84+
```
85+
86+
With some UI frameworks, these compiler-generated views implement quite a few interfaces, so unless you need them, it's probably best to register these classes `.AsSelf()`; in other words, be very precise with your filters that accept compiler generated types.
87+
6988
### Decoration
7089

7190
```csharp

0 commit comments

Comments
 (0)