-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathImplicitPropertyInjectionExtensions.cs
More file actions
43 lines (34 loc) · 1.63 KB
/
ImplicitPropertyInjectionExtensions.cs
File metadata and controls
43 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// <auto-generated />
namespace SimpleInjector.CodeSamples
{
using System;
using System.Diagnostics;
using System.Reflection;
using SimpleInjector.Advanced;
public static class ImplicitPropertyInjectionExtensions
{
[DebuggerStepThrough]
public static void AutoWirePropertiesImplicitly(this ContainerOptions options)
{
options.PropertySelectionBehavior = new ImplicitPropertyInjectionBehavior(options.Container);
}
}
public class ImplicitPropertyInjectionBehavior : IPropertySelectionBehavior {
private readonly IPropertySelectionBehavior original;
private readonly ContainerOptions options;
internal ImplicitPropertyInjectionBehavior(Container container) {
this.options = container.Options;
this.original = container.Options.PropertySelectionBehavior;
}
public bool SelectProperty(Type t, PropertyInfo p) =>
this.IsImplicitInjectable(t, p) || this.original.SelectProperty(t, p);
private bool IsImplicitInjectable(Type t, PropertyInfo p) =>
IsInjectableProperty(p) && this.CanBeResolved(t, p);
private static bool IsInjectableProperty(PropertyInfo property) =>
property.CanWrite && property.GetSetMethod(nonPublic: false)?.IsStatic == false;
private bool CanBeResolved(Type t, PropertyInfo property) =>
this.GetProducer(new InjectionConsumerInfo(t, property)) != null;
private InstanceProducer GetProducer(InjectionConsumerInfo info) =>
this.options.DependencyInjectionBehavior.GetInstanceProducer(info, false);
}
}