|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Linq.Dynamic.Core.Validation; |
3 | 3 | using System.Linq.Expressions; |
| 4 | +using System.Threading; |
4 | 5 | using AnyOfTypes; |
5 | 6 |
|
6 | 7 | namespace System.Linq.Dynamic.Core.Parser; |
@@ -32,7 +33,7 @@ internal class KeywordsHelper : IKeywordsHelper |
32 | 33 | private static readonly Dictionary<string, Type> PreDefinedTypeMapping = new(); |
33 | 34 |
|
34 | 35 | // Custom DefinedTypes are not IgnoreCase |
35 | | - private readonly Dictionary<string, Type> _customTypeMapping = new(); |
| 36 | + private readonly Lazy<Dictionary<string, Type>> _customTypeMapping; |
36 | 37 |
|
37 | 38 | static KeywordsHelper() |
38 | 39 | { |
@@ -66,22 +67,27 @@ public KeywordsHelper(ParsingConfig config) |
66 | 67 | { FUNCTION_CAST, FUNCTION_CAST } |
67 | 68 | }; |
68 | 69 |
|
| 70 | + _customTypeMapping = new Lazy<Dictionary<string, Type>>(() => |
| 71 | + { |
| 72 | + Dictionary<string, Type> customTypeMapping = new(); |
| 73 | + if (config.CustomTypeProvider != null) |
| 74 | + { |
| 75 | + foreach (var type in config.CustomTypeProvider.GetCustomTypes()) |
| 76 | + { |
| 77 | + customTypeMapping[type.FullName!] = type; |
| 78 | + customTypeMapping[type.Name] = type; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return customTypeMapping; |
| 83 | + }, LazyThreadSafetyMode.PublicationOnly); |
| 84 | + |
69 | 85 | if (config.AreContextKeywordsEnabled) |
70 | 86 | { |
71 | 87 | _mappings.Add(KEYWORD_IT, KEYWORD_IT); |
72 | 88 | _mappings.Add(KEYWORD_PARENT, KEYWORD_PARENT); |
73 | 89 | _mappings.Add(KEYWORD_ROOT, KEYWORD_ROOT); |
74 | 90 | } |
75 | | - |
76 | | - // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract |
77 | | - if (config.CustomTypeProvider != null) |
78 | | - { |
79 | | - foreach (var type in config.CustomTypeProvider.GetCustomTypes()) |
80 | | - { |
81 | | - _customTypeMapping[type.FullName!] = type; |
82 | | - _customTypeMapping[type.Name] = type; |
83 | | - } |
84 | | - } |
85 | 91 | } |
86 | 92 |
|
87 | 93 | public bool IsItOrRootOrParent(AnyOf<string, Expression, Type> value) |
@@ -125,7 +131,7 @@ public bool TryGetValue(string text, out AnyOf<string, Expression, Type> value) |
125 | 131 | } |
126 | 132 |
|
127 | 133 | // 5. Try to get as custom type |
128 | | - if (_customTypeMapping.TryGetValue(text, out var customType)) |
| 134 | + if (_customTypeMapping.Value.TryGetValue(text, out var customType)) |
129 | 135 | { |
130 | 136 | value = customType; |
131 | 137 | return true; |
|
0 commit comments