@@ -12,18 +12,21 @@ namespace Nest
1212{
1313 internal static class TypeExtensions
1414 {
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 ) ;
1617
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 > > ( ) ;
1920
21+ private static readonly ConcurrentDictionary < string , Type > CachedGenericClosedTypes =
22+ new ConcurrentDictionary < string , Type > ( ) ;
2023
21- private static ConcurrentDictionary < Type , IList < JsonProperty > > _cachedTypeProperties =
24+ private static readonly ConcurrentDictionary < Type , IList < JsonProperty > > CachedTypeProperties =
2225 new ConcurrentDictionary < Type , IList < JsonProperty > > ( ) ;
2326
2427 //this contract is only used to resolve properties in class WE OWN.
2528 //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 ) ;
2730
2831 public delegate T ObjectActivator < out T > ( params object [ ] args ) ;
2932
@@ -37,10 +40,10 @@ internal static object CreateGenericInstance(this Type t, Type[] closeOver, para
3740 var argKey = closeOver . Aggregate ( new StringBuilder ( ) , ( sb , gt ) => sb . Append ( "--" + gt . FullName ) , sb => sb . ToString ( ) ) ;
3841 var key = t . FullName + argKey ;
3942 Type closedType ;
40- if ( ! _cachedGenericClosedTypes . TryGetValue ( key , out closedType ) )
43+ if ( ! CachedGenericClosedTypes . TryGetValue ( key , out closedType ) )
4144 {
4245 closedType = t . MakeGenericType ( closeOver ) ;
43- _cachedGenericClosedTypes . TryAdd ( key , closedType ) ;
46+ CachedGenericClosedTypes . TryAdd ( key , closedType ) ;
4447 }
4548 return closedType . CreateInstance ( args ) ;
4649 }
@@ -50,25 +53,23 @@ internal static object CreateGenericInstance(this Type t, Type[] closeOver, para
5053 internal static object CreateInstance ( this Type t , params object [ ] args )
5154 {
5255 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 ;
5657 var key = argKey + "--" + t . FullName ;
57- if ( _cachedActivators . TryGetValue ( key , out activator ) )
58+ if ( CachedActivators . TryGetValue ( key , out activator ) )
5859 return activator ( args ) ;
59- var generic = GetActivatorMethodInfo . MakeGenericMethod ( t ) ;
6060
61+ var generic = GetActivatorMethodInfo . MakeGenericMethod ( t ) ;
6162 var constructors = from c in t . GetConstructors ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance )
6263 let p = c . GetParameters ( )
6364 let k = string . Join ( "," , p . Select ( a => a . ParameterType . Name ) )
64- where p . Count ( ) == argLength //&& k == argKey
65+ where p . Length == args . Length
6566 select c ;
67+
6668 var ctor = constructors . FirstOrDefault ( ) ;
6769 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") ;
7071 activator = ( ObjectActivator < object > ) generic . Invoke ( null , new [ ] { ctor } ) ;
71- _cachedActivators . TryAdd ( key , activator ) ;
72+ CachedActivators . TryAdd ( key , activator ) ;
7273 return activator ( args ) ;
7374 }
7475
@@ -85,7 +86,7 @@ private static ObjectActivator<T> GetActivator<T>(ConstructorInfo ctor)
8586 Expression [ ] argsExp =
8687 new Expression [ paramsInfo . Length ] ;
8788
88- //pick each arg from the params array
89+ //pick each arg from the params array
8990 //and create a typed expression of them
9091 for ( int i = 0 ; i < paramsInfo . Length ; i ++ )
9192 {
@@ -118,10 +119,10 @@ private static ObjectActivator<T> GetActivator<T>(ConstructorInfo ctor)
118119 internal static IList < JsonProperty > GetCachedObjectProperties ( this Type t , MemberSerialization memberSerialization = MemberSerialization . OptIn )
119120 {
120121 IList < JsonProperty > propertyDictionary ;
121- if ( _cachedTypeProperties . TryGetValue ( t , out propertyDictionary ) )
122+ if ( CachedTypeProperties . TryGetValue ( t , out propertyDictionary ) )
122123 return propertyDictionary ;
123- propertyDictionary = _jsonContract . PropertiesOfAll ( t , memberSerialization ) ;
124- _cachedTypeProperties . TryAdd ( t , propertyDictionary ) ;
124+ propertyDictionary = JsonContract . PropertiesOfAll ( t , memberSerialization ) ;
125+ CachedTypeProperties . TryAdd ( t , propertyDictionary ) ;
125126 return propertyDictionary ;
126127 }
127128
@@ -163,4 +164,4 @@ internal static IEnumerable<Type> GetInterfaces(this Type type)
163164 }
164165#endif
165166 }
166- }
167+ }
0 commit comments