1- /* Copyright 2018 -present MongoDB Inc.
2- *
3- * Licensed under the Apache License, Version 2.0 (the "License");
4- * you may not use this file except in compliance with the License.
5- * You may obtain a copy of the License at
6- *
7- * http://www.apache.org/licenses/LICENSE-2.0
8- *
9- * Unless required by applicable law or agreed to in writing, software
10- * distributed under the License is distributed on an "AS IS" BASIS,
11- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12- * See the License for the specific language governing permissions and
13- * limitations under the License.
14- */
1+ /* Copyright 2010 -present MongoDB Inc.
2+ *
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
1515
1616using System ;
1717using System . Collections . Generic ;
@@ -40,13 +40,19 @@ public static object GetStaticFieldValue(Type type, string name, BindingFlags fl
4040 return fieldInfo . GetValue ( null ) ;
4141 }
4242
43+ public static TFieldType GetStaticFieldValue < TObjectType , TFieldType > ( string name , BindingFlags flags = BindingFlags . NonPublic | BindingFlags . Static )
44+ {
45+ var fieldInfo = GetDeclaredOrInheritedField ( typeof ( TObjectType ) , name , flags ) ;
46+ return ( TFieldType ) fieldInfo . GetValue ( null ) ;
47+ }
48+
4349 public static object Invoke ( object obj , string name , BindingFlags flags = BindingFlags . NonPublic | BindingFlags . Instance )
4450 {
4551 try
4652 {
47- var methodInfo = obj . GetType ( ) . GetMethods ( flags )
48- . Where ( m => m . Name == name && m . GetParameters ( ) . Length == 0 )
49- . Single ( ) ;
53+ var methodInfo = obj . GetType ( )
54+ . GetMethods ( flags )
55+ . Single ( m => m . Name == name && m . GetParameters ( ) . Length == 0 ) ;
5056 return methodInfo . Invoke ( obj , new object [ ] { } ) ;
5157 }
5258 catch ( TargetInvocationException exception )
@@ -58,9 +64,9 @@ public static object Invoke(object obj, string name, BindingFlags flags = Bindin
5864 public static object Invoke < T1 > ( object obj , string name , T1 arg1 )
5965 {
6066 var parameterTypes = new [ ] { typeof ( T1 ) } ;
61- var methodInfo = obj . GetType ( ) . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
62- . Where ( m => m . Name == name && m . GetParameters ( ) . Select ( p => p . ParameterType ) . SequenceEqual ( parameterTypes ) )
63- . Single ( ) ;
67+ var methodInfo = obj . GetType ( )
68+ . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
69+ . Single ( m => m . Name == name && m . GetParameters ( ) . Select ( p => p . ParameterType ) . SequenceEqual ( parameterTypes ) ) ;
6470 try
6571 {
6672 return methodInfo . Invoke ( obj , new object [ ] { arg1 } ) ;
0 commit comments