@@ -12,18 +12,21 @@ namespace Nest
12
12
{
13
13
internal static class TypeExtensions
14
14
{
15
- private static MethodInfo GetActivatorMethodInfo = typeof ( TypeExtensions ) . GetMethod ( "GetActivator" , BindingFlags . Static | BindingFlags . NonPublic ) ;
15
+ private static readonly MethodInfo GetActivatorMethodInfo =
16
+ typeof ( TypeExtensions ) . GetMethod ( nameof ( GetActivator ) , BindingFlags . Static | BindingFlags . NonPublic ) ;
16
17
17
- private static ConcurrentDictionary < string , ObjectActivator < object > > _cachedActivators = new ConcurrentDictionary < string , ObjectActivator < object > > ( ) ;
18
- private static ConcurrentDictionary < string , Type > _cachedGenericClosedTypes = new ConcurrentDictionary < string , Type > ( ) ;
18
+ private static readonly ConcurrentDictionary < string , ObjectActivator < object > > CachedActivators =
19
+ new ConcurrentDictionary < string , ObjectActivator < object > > ( ) ;
19
20
21
+ private static readonly ConcurrentDictionary < string , Type > CachedGenericClosedTypes =
22
+ new ConcurrentDictionary < string , Type > ( ) ;
20
23
21
- private static ConcurrentDictionary < Type , IList < JsonProperty > > _cachedTypeProperties =
24
+ private static readonly ConcurrentDictionary < Type , IList < JsonProperty > > CachedTypeProperties =
22
25
new ConcurrentDictionary < Type , IList < JsonProperty > > ( ) ;
23
26
24
27
//this contract is only used to resolve properties in class WE OWN.
25
28
//these are not subject to change depending on what the user passes as connectionsettings
26
- private static ElasticContractResolver _jsonContract = new ElasticContractResolver ( new ConnectionSettings ( ) , null ) ;
29
+ private static readonly ElasticContractResolver JsonContract = new ElasticContractResolver ( new ConnectionSettings ( ) , null ) ;
27
30
28
31
public delegate T ObjectActivator < out T > ( params object [ ] args ) ;
29
32
@@ -37,10 +40,10 @@ internal static object CreateGenericInstance(this Type t, Type[] closeOver, para
37
40
var argKey = closeOver . Aggregate ( new StringBuilder ( ) , ( sb , gt ) => sb . Append ( "--" + gt . FullName ) , sb => sb . ToString ( ) ) ;
38
41
var key = t . FullName + argKey ;
39
42
Type closedType ;
40
- if ( ! _cachedGenericClosedTypes . TryGetValue ( key , out closedType ) )
43
+ if ( ! CachedGenericClosedTypes . TryGetValue ( key , out closedType ) )
41
44
{
42
45
closedType = t . MakeGenericType ( closeOver ) ;
43
- _cachedGenericClosedTypes . TryAdd ( key , closedType ) ;
46
+ CachedGenericClosedTypes . TryAdd ( key , closedType ) ;
44
47
}
45
48
return closedType . CreateInstance ( args ) ;
46
49
}
@@ -50,25 +53,23 @@ internal static object CreateGenericInstance(this Type t, Type[] closeOver, para
50
53
internal static object CreateInstance ( this Type t , params object [ ] args )
51
54
{
52
55
ObjectActivator < object > activator ;
53
- var argLength = args . Count ( ) ;
54
- //var argKey = string.Join(",", args.Select(a => a.GetType().Name));
55
- var argKey = argLength ;
56
+ var argKey = args . Length ;
56
57
var key = argKey + "--" + t . FullName ;
57
- if ( _cachedActivators . TryGetValue ( key , out activator ) )
58
+ if ( CachedActivators . TryGetValue ( key , out activator ) )
58
59
return activator ( args ) ;
59
- var generic = GetActivatorMethodInfo . MakeGenericMethod ( t ) ;
60
60
61
+ var generic = GetActivatorMethodInfo . MakeGenericMethod ( t ) ;
61
62
var constructors = from c in t . GetConstructors ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance )
62
63
let p = c . GetParameters ( )
63
64
let k = string . Join ( "," , p . Select ( a => a . ParameterType . Name ) )
64
- where p . Count ( ) == argLength //&& k == argKey
65
+ where p . Length == args . Length
65
66
select c ;
67
+
66
68
var ctor = constructors . FirstOrDefault ( ) ;
67
69
if ( ctor == null )
68
- throw new Exception ( "Cannot create an instance of " + t . FullName
69
- + " because it has no constructor taking " + argLength + " arguments" ) ;
70
+ throw new Exception ( $ "Cannot create an instance of { t . FullName } because it has no constructor taking { args . Length } arguments") ;
70
71
activator = ( ObjectActivator < object > ) generic . Invoke ( null , new [ ] { ctor } ) ;
71
- _cachedActivators . TryAdd ( key , activator ) ;
72
+ CachedActivators . TryAdd ( key , activator ) ;
72
73
return activator ( args ) ;
73
74
}
74
75
@@ -85,7 +86,7 @@ private static ObjectActivator<T> GetActivator<T>(ConstructorInfo ctor)
85
86
Expression [ ] argsExp =
86
87
new Expression [ paramsInfo . Length ] ;
87
88
88
- //pick each arg from the params array
89
+ //pick each arg from the params array
89
90
//and create a typed expression of them
90
91
for ( int i = 0 ; i < paramsInfo . Length ; i ++ )
91
92
{
@@ -118,10 +119,10 @@ private static ObjectActivator<T> GetActivator<T>(ConstructorInfo ctor)
118
119
internal static IList < JsonProperty > GetCachedObjectProperties ( this Type t , MemberSerialization memberSerialization = MemberSerialization . OptIn )
119
120
{
120
121
IList < JsonProperty > propertyDictionary ;
121
- if ( _cachedTypeProperties . TryGetValue ( t , out propertyDictionary ) )
122
+ if ( CachedTypeProperties . TryGetValue ( t , out propertyDictionary ) )
122
123
return propertyDictionary ;
123
- propertyDictionary = _jsonContract . PropertiesOfAll ( t , memberSerialization ) ;
124
- _cachedTypeProperties . TryAdd ( t , propertyDictionary ) ;
124
+ propertyDictionary = JsonContract . PropertiesOfAll ( t , memberSerialization ) ;
125
+ CachedTypeProperties . TryAdd ( t , propertyDictionary ) ;
125
126
return propertyDictionary ;
126
127
}
127
128
@@ -163,4 +164,4 @@ internal static IEnumerable<Type> GetInterfaces(this Type type)
163
164
}
164
165
#endif
165
166
}
166
- }
167
+ }
0 commit comments