@@ -12,9 +12,9 @@ public class PolicyList : IPolicyList
12
12
{
13
13
#region Fields
14
14
15
+ private readonly object _sync = new object ( ) ;
15
16
private readonly IPolicyList _innerPolicyList ;
16
- private readonly IDictionary < PolicyKey , IBuilderPolicy > _policies =
17
- new ConcurrentDictionary < PolicyKey , IBuilderPolicy > ( PolicyKeyEqualityComparer . Default ) ;
17
+ private IDictionary < PolicyKey , IBuilderPolicy > _policies = null ;
18
18
19
19
#endregion
20
20
@@ -47,20 +47,20 @@ public PolicyList(IPolicyList innerPolicyList)
47
47
/// <value>
48
48
/// The number of items in the locator.
49
49
/// </value>
50
- public int Count => _policies . Count ;
50
+ public int Count => _policies ? . Count ?? 0 ;
51
51
52
52
53
53
public void Clear ( Type type , string name , Type policyInterface )
54
54
{
55
- _policies . Remove ( new PolicyKey ( type , name , policyInterface ) ) ;
55
+ _policies ? . Remove ( new PolicyKey ( type , name , policyInterface ) ) ;
56
56
}
57
57
58
58
/// <summary>
59
59
/// Removes all policies from the list.
60
60
/// </summary>
61
61
public void ClearAll ( )
62
62
{
63
- _policies . Clear ( ) ;
63
+ _policies = null ;
64
64
}
65
65
66
66
/// <summary>
@@ -76,8 +76,9 @@ public void ClearDefault(Type policyInterface)
76
76
public IBuilderPolicy Get ( Type type , string name , Type policyInterface , out IPolicyList list )
77
77
{
78
78
list = null ;
79
+ IBuilderPolicy policy = null ;
79
80
80
- if ( 0 < _policies . Count && _policies . TryGetValue ( new PolicyKey ( type , name , policyInterface ) , out var policy ) )
81
+ if ( _policies ? . TryGetValue ( new PolicyKey ( type , name , policyInterface ) , out policy ) ?? false )
81
82
{
82
83
list = this ;
83
84
return policy ;
@@ -89,6 +90,14 @@ public IBuilderPolicy Get(Type type, string name, Type policyInterface, out IPol
89
90
90
91
public void Set ( Type type , string name , Type policyInterface , IBuilderPolicy policy )
91
92
{
93
+ if ( null == _policies )
94
+ {
95
+ lock ( _sync )
96
+ {
97
+ if ( null == _policies )
98
+ _policies = new ConcurrentDictionary < PolicyKey , IBuilderPolicy > ( PolicyKeyEqualityComparer . Default ) ;
99
+ }
100
+ }
92
101
_policies [ new PolicyKey ( type , name , policyInterface ) ] = policy ;
93
102
}
94
103
0 commit comments