PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/Microsoft.TestCommon/Microsoft/TestCommon/GenericTypeAssert.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 489 lines | 259 code | 44 blank | 186 comment | 19 complexity | 40c27cdfa26c3b2f64d56f7c62517299 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Xunit;
  5. namespace Microsoft.TestCommon
  6. {
  7. /// <summary>
  8. /// MSTest assertion class to provide convenience and assert methods for generic types
  9. /// whose type parameters are not known at compile time.
  10. /// </summary>
  11. public class GenericTypeAssert
  12. {
  13. private static readonly GenericTypeAssert singleton = new GenericTypeAssert();
  14. public static GenericTypeAssert Singleton { get { return singleton; } }
  15. /// <summary>
  16. /// Asserts the given <paramref name="genericBaseType"/> is a generic type and creates a new
  17. /// bound generic type using <paramref name="genericParameterType"/>. It then asserts there
  18. /// is a constructor that will accept <paramref name="parameterTypes"/> and returns it.
  19. /// </summary>
  20. /// <param name="genericBaseType">The unbound generic base type.</param>
  21. /// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
  22. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  23. /// <returns>The <see cref="ConstructorInfo"/> of that constructor which may be invoked to create that new generic type.</returns>
  24. public ConstructorInfo GetConstructor(Type genericBaseType, Type genericParameterType, params Type[] parameterTypes)
  25. {
  26. Assert.NotNull(genericBaseType);
  27. Assert.True(genericBaseType.IsGenericTypeDefinition);
  28. Assert.NotNull(genericParameterType);
  29. Assert.NotNull(parameterTypes);
  30. Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType });
  31. ConstructorInfo ctor = genericType.GetConstructor(parameterTypes);
  32. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name));
  33. return ctor;
  34. }
  35. /// <summary>
  36. /// Asserts the given <paramref name="genericBaseType"/> is a generic type and creates a new
  37. /// bound generic type using <paramref name="genericParameterType"/>. It then asserts there
  38. /// is a constructor that will accept <paramref name="parameterTypes"/> and returns it.
  39. /// </summary>
  40. /// <param name="genericBaseType">The unbound generic base type.</param>
  41. /// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
  42. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  43. /// <returns>The <see cref="ConstructorInfo"/> of that constructor which may be invoked to create that new generic type.</returns>
  44. public ConstructorInfo GetConstructor(Type genericBaseType, Type[] genericParameterTypes, params Type[] parameterTypes)
  45. {
  46. Assert.NotNull(genericBaseType);
  47. Assert.True(genericBaseType.IsGenericTypeDefinition);
  48. Assert.NotNull(genericParameterTypes);
  49. Assert.NotNull(parameterTypes);
  50. Type genericType = genericBaseType.MakeGenericType(genericParameterTypes);
  51. ConstructorInfo ctor = genericType.GetConstructor(parameterTypes);
  52. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name));
  53. return ctor;
  54. }
  55. /// <summary>
  56. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  57. /// </summary>
  58. /// <param name="genericBaseType">The unbound generic base type.</param>
  59. /// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
  60. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  61. /// <param name="parameterValues">The list of values to supply to the constructor</param>
  62. /// <returns>The instance created by calling that constructor.</returns>
  63. public object InvokeConstructor(Type genericBaseType, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
  64. {
  65. ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterType, parameterTypes);
  66. Assert.NotNull(parameterValues);
  67. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  68. return ctor.Invoke(parameterValues);
  69. }
  70. /// <summary>
  71. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  72. /// </summary>
  73. /// <param name="genericBaseType">The unbound generic base type.</param>
  74. /// <param name="genericParameterTypse">The types of the generic parameters to apply to create a bound generic type.</param>
  75. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  76. /// <param name="parameterValues">The list of values to supply to the constructor</param>
  77. /// <returns>The instance created by calling that constructor.</returns>
  78. public object InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, Type[] parameterTypes, object[] parameterValues)
  79. {
  80. ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterTypes, parameterTypes);
  81. Assert.NotNull(parameterValues);
  82. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  83. return ctor.Invoke(parameterValues);
  84. }
  85. /// <summary>
  86. /// Creates a new bound generic type and invokes the constructor matched from the types of <paramref name="parameterValues"/>.
  87. /// </summary>
  88. /// <param name="genericBaseType">The unbound generic base type.</param>
  89. /// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
  90. /// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
  91. /// <returns>The instance created by calling that constructor.</returns>
  92. public object InvokeConstructor(Type genericBaseType, Type genericParameterType, params object[] parameterValues)
  93. {
  94. Assert.NotNull(genericBaseType);
  95. Assert.True(genericBaseType.IsGenericTypeDefinition);
  96. Assert.NotNull(genericParameterType);
  97. Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType });
  98. ConstructorInfo ctor = FindConstructor(genericType, parameterValues);
  99. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name));
  100. return ctor.Invoke(parameterValues);
  101. }
  102. /// <summary>
  103. /// Creates a new bound generic type and invokes the constructor matched from the types of <paramref name="parameterValues"/>.
  104. /// </summary>
  105. /// <param name="genericBaseType">The unbound generic base type.</param>
  106. /// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
  107. /// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
  108. /// <returns>The instance created by calling that constructor.</returns>
  109. public object InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, params object[] parameterValues)
  110. {
  111. Assert.NotNull(genericBaseType);
  112. Assert.True(genericBaseType.IsGenericTypeDefinition);
  113. Assert.NotNull(genericParameterTypes);
  114. Type genericType = genericBaseType.MakeGenericType(genericParameterTypes);
  115. ConstructorInfo ctor = FindConstructor(genericType, parameterValues);
  116. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name));
  117. return ctor.Invoke(parameterValues);
  118. }
  119. /// <summary>
  120. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  121. /// </summary>
  122. /// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
  123. /// <param name="genericBaseType">The unbound generic base type.</param>
  124. /// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
  125. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  126. /// <param name="parameterValues">The list of values to supply to the constructor</param>
  127. /// <returns>An instance of type <typeparamref name="T"/>.</returns>
  128. public T InvokeConstructor<T>(Type genericBaseType, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
  129. {
  130. ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterType, parameterTypes);
  131. Assert.NotNull(parameterValues);
  132. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  133. return (T)ctor.Invoke(parameterValues);
  134. }
  135. /// <summary>
  136. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  137. /// </summary>
  138. /// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
  139. /// <param name="genericBaseType">The unbound generic base type.</param>
  140. /// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
  141. /// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
  142. /// <param name="parameterValues">The list of values to supply to the constructor</param>
  143. /// <returns>An instance of type <typeparamref name="T"/>.</returns>
  144. public T InvokeConstructor<T>(Type genericBaseType, Type[] genericParameterTypes, Type[] parameterTypes, object[] parameterValues)
  145. {
  146. ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterTypes, parameterTypes);
  147. Assert.NotNull(parameterValues);
  148. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  149. return (T)ctor.Invoke(parameterValues);
  150. }
  151. /// <summary>
  152. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  153. /// </summary>
  154. /// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
  155. /// <param name="genericBaseType">The unbound generic base type.</param>
  156. /// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
  157. /// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
  158. /// <returns>The instance created by calling that constructor.</returns>
  159. /// <returns>An instance of type <typeparamref name="T"/>.</returns>
  160. public T InvokeConstructor<T>(Type genericBaseType, Type genericParameterType, params object[] parameterValues)
  161. {
  162. Assert.NotNull(genericBaseType == null);
  163. Assert.True(genericBaseType.IsGenericTypeDefinition);
  164. Assert.NotNull(genericParameterType);
  165. Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType });
  166. ConstructorInfo ctor = FindConstructor(genericType, parameterValues);
  167. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name));
  168. return (T)ctor.Invoke(parameterValues);
  169. }
  170. /// <summary>
  171. /// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
  172. /// </summary>
  173. /// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
  174. /// <param name="genericBaseType">The unbound generic base type.</param>
  175. /// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
  176. /// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
  177. /// <returns>The instance created by calling that constructor.</returns>
  178. /// <returns>An instance of type <typeparamref name="T"/>.</returns>
  179. public T InvokeConstructor<T>(Type genericBaseType, Type[] genericParameterTypes, params object[] parameterValues)
  180. {
  181. Assert.NotNull(genericBaseType);
  182. Assert.True(genericBaseType.IsGenericTypeDefinition);
  183. Assert.NotNull(genericParameterTypes);
  184. Type genericType = genericBaseType.MakeGenericType(genericParameterTypes);
  185. ConstructorInfo ctor = FindConstructor(genericType, parameterValues);
  186. Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name));
  187. return (T)ctor.Invoke(parameterValues);
  188. }
  189. /// <summary>
  190. /// Asserts the given instance is one from a generic type of the specified parameter type.
  191. /// </summary>
  192. /// <typeparam name="T">The type of instance.</typeparam>
  193. /// <param name="instance">The instance to test.</param>
  194. /// <param name="genericTypeParameter">The type of the generic parameter to which the instance's generic type should have been bound.</param>
  195. public void IsCorrectGenericType<T>(T instance, Type genericTypeParameter)
  196. {
  197. Assert.NotNull(instance);
  198. Assert.NotNull(genericTypeParameter);
  199. Assert.True(instance.GetType().IsGenericType);
  200. Type[] genericArguments = instance.GetType().GetGenericArguments();
  201. Assert.Equal(1, genericArguments.Length);
  202. Assert.Equal(genericTypeParameter, genericArguments[0]);
  203. }
  204. /// <summary>
  205. /// Invokes via Reflection the method on the given instance.
  206. /// </summary>
  207. /// <param name="instance">The instance to use.</param>
  208. /// <param name="methodName">The name of the method to call.</param>
  209. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  210. /// <param name="parameterValues">The values to supply to the method.</param>
  211. /// <returns>The results of the method.</returns>
  212. public object InvokeMethod(object instance, string methodName, Type[] parameterTypes, object[] parameterValues)
  213. {
  214. Assert.NotNull(instance);
  215. Assert.NotNull(parameterTypes);
  216. Assert.NotNull(parameterValues);
  217. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  218. MethodInfo methodInfo = instance.GetType().GetMethod(methodName, parameterTypes);
  219. Assert.NotNull(methodInfo);
  220. return methodInfo.Invoke(instance, parameterValues);
  221. }
  222. /// <summary>
  223. /// Invokes via Reflection the static method on the given type.
  224. /// </summary>
  225. /// <param name="type">The type containing the method.</param>
  226. /// <param name="methodName">The name of the method to call.</param>
  227. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  228. /// <param name="parameterValues">The values to supply to the method.</param>
  229. /// <returns>The results of the method.</returns>
  230. public object InvokeMethod(Type type, string methodName, Type[] parameterTypes, object[] parameterValues)
  231. {
  232. Assert.NotNull(type);
  233. Assert.NotNull(parameterTypes);
  234. Assert.NotNull(parameterValues);
  235. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  236. MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes);
  237. Assert.NotNull(methodInfo);
  238. return methodInfo.Invoke(null, parameterValues);
  239. }
  240. /// <summary>
  241. /// Invokes via Reflection the static method on the given type.
  242. /// </summary>
  243. /// <param name="type">The type containing the method.</param>
  244. /// <param name="methodName">The name of the method to call.</param>
  245. /// <param name="genericParameterType">The generic parameter type of the method.</param>
  246. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  247. /// <param name="parameterValues">The values to supply to the method.</param>
  248. /// <returns>The results of the method.</returns>
  249. public MethodInfo CreateGenericMethod(Type type, string methodName, Type genericParameterType, Type[] parameterTypes)
  250. {
  251. Assert.NotNull(type);
  252. Assert.NotNull(parameterTypes);
  253. Assert.NotNull(genericParameterType);
  254. //MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes);
  255. MethodInfo methodInfo = type.GetMethods().Where((m) => m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase) && m.IsGenericMethod && AreAssignableFrom(m.GetParameters(), parameterTypes)).FirstOrDefault();
  256. Assert.NotNull(methodInfo);
  257. Assert.True(methodInfo.IsGenericMethod);
  258. MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType);
  259. Assert.NotNull(genericMethod);
  260. return genericMethod;
  261. }
  262. /// <summary>
  263. /// Invokes via Reflection the static generic method on the given type.
  264. /// </summary>
  265. /// <param name="type">The type containing the method.</param>
  266. /// <param name="methodName">The name of the method to call.</param>
  267. /// <param name="genericParameterType">The generic parameter type of the method.</param>
  268. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  269. /// <param name="parameterValues">The values to supply to the method.</param>
  270. /// <returns>The results of the method.</returns>
  271. public object InvokeGenericMethod(Type type, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
  272. {
  273. MethodInfo methodInfo = CreateGenericMethod(type, methodName, genericParameterType, parameterTypes);
  274. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  275. return methodInfo.Invoke(null, parameterValues);
  276. }
  277. /// <summary>
  278. /// Invokes via Reflection the generic method on the given instance.
  279. /// </summary>
  280. /// <param name="instance">The instance on which to invoke the method.</param>
  281. /// <param name="methodName">The name of the method to call.</param>
  282. /// <param name="genericParameterType">The generic parameter type of the method.</param>
  283. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  284. /// <param name="parameterValues">The values to supply to the method.</param>
  285. /// <returns>The results of the method.</returns>
  286. public object InvokeGenericMethod(object instance, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
  287. {
  288. Assert.NotNull(instance);
  289. MethodInfo methodInfo = CreateGenericMethod(instance.GetType(), methodName, genericParameterType, parameterTypes);
  290. Assert.Equal(parameterTypes.Length, parameterValues.Length);
  291. return methodInfo.Invoke(instance, parameterValues);
  292. }
  293. /// <summary>
  294. /// Invokes via Reflection the generic method on the given instance.
  295. /// </summary>
  296. /// <typeparam name="T">The type of the return value from the method.</typeparam>
  297. /// <param name="instance">The instance on which to invoke the method.</param>
  298. /// <param name="methodName">The name of the method to call.</param>
  299. /// <param name="genericParameterType">The generic parameter type of the method.</param>
  300. /// <param name="parameterTypes">The types of the parameters to the method.</param>
  301. /// <param name="parameterValues">The values to supply to the method.</param>
  302. /// <returns>The results of the method.</returns>
  303. public T InvokeGenericMethod<T>(object instance, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
  304. {
  305. return (T)InvokeGenericMethod(instance, methodName, genericParameterType, parameterTypes, parameterValues);
  306. }
  307. /// <summary>
  308. /// Invokes via Reflection the method on the given instance.
  309. /// </summary>
  310. /// <param name="instance">The instance to use.</param>
  311. /// <param name="methodName">The name of the method to call.</param>
  312. /// <param name="parameterValues">The values to supply to the method.</param>
  313. /// <returns>The results of the method.</returns>
  314. public object InvokeMethod(object instance, string methodName, params object[] parameterValues)
  315. {
  316. Assert.NotNull(instance);
  317. MethodInfo methodInfo = FindMethod(instance.GetType(), methodName, parameterValues);
  318. Assert.NotNull(methodInfo);
  319. return methodInfo.Invoke(instance, parameterValues);
  320. }
  321. /// <summary>
  322. /// Invokes via Reflection the static method on the given type.
  323. /// </summary>
  324. /// <param name="instance">The instance to use.</param>
  325. /// <param name="methodName">The name of the method to call.</param>
  326. /// <param name="parameterValues">The values to supply to the method.</param>
  327. /// <returns>The results of the method.</returns>
  328. public object InvokeMethod(Type type, string methodName, params object[] parameterValues)
  329. {
  330. Assert.NotNull(type);
  331. MethodInfo methodInfo = FindMethod(type, methodName, parameterValues);
  332. Assert.NotNull(methodInfo);
  333. return methodInfo.Invoke(null, parameterValues);
  334. }
  335. /// <summary>
  336. /// Invokes via Reflection the method on the given instance.
  337. /// </summary>
  338. /// <param name="instance">The instance to use.</param>
  339. /// <param name="methodName">The name of the method to call.</param>
  340. /// <param name="genericParameterType">The type of the generic parameter.</param>
  341. /// <param name="parameterValues">The values to supply to the method.</param>
  342. /// <returns>The results of the method.</returns>
  343. public object InvokeGenericMethod(object instance, string methodName, Type genericParameterType, params object[] parameterValues)
  344. {
  345. Assert.NotNull(instance);
  346. Assert.NotNull(genericParameterType);
  347. MethodInfo methodInfo = FindMethod(instance.GetType(), methodName, parameterValues);
  348. Assert.NotNull(methodInfo);
  349. Assert.True(methodInfo.IsGenericMethod);
  350. MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType);
  351. return genericMethod.Invoke(instance, parameterValues);
  352. }
  353. /// <summary>
  354. /// Invokes via Reflection the method on the given instance.
  355. /// </summary>
  356. /// <param name="instance">The instance to use.</param>
  357. /// <param name="methodName">The name of the method to call.</param>
  358. /// <param name="genericParameterType">The type of the generic parameter.</param>
  359. /// <param name="parameterValues">The values to supply to the method.</param>
  360. /// <returns>The results of the method.</returns>
  361. public object InvokeGenericMethod(Type type, string methodName, Type genericParameterType, params object[] parameterValues)
  362. {
  363. Assert.NotNull(type);
  364. Assert.NotNull(genericParameterType);
  365. MethodInfo methodInfo = FindMethod(type, methodName, parameterValues);
  366. Assert.NotNull(methodInfo);
  367. Assert.True(methodInfo.IsGenericMethod);
  368. MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType);
  369. return genericMethod.Invoke(null, parameterValues);
  370. }
  371. /// <summary>
  372. /// Retrieves the value from the specified property.
  373. /// </summary>
  374. /// <param name="instance">The instance containing the property value.</param>
  375. /// <param name="propertyName">The name of the property.</param>
  376. /// <param name="failureMessage">The error message to prefix any test assertions.</param>
  377. /// <returns>The value returned from the property.</returns>
  378. public object GetProperty(object instance, string propertyName, string failureMessage)
  379. {
  380. PropertyInfo propertyInfo = instance.GetType().GetProperty(propertyName);
  381. Assert.NotNull(propertyInfo);
  382. return propertyInfo.GetValue(instance, null);
  383. }
  384. private static bool AreAssignableFrom(Type[] parameterTypes, params object[] parameterValues)
  385. {
  386. Assert.NotNull(parameterTypes);
  387. Assert.NotNull(parameterValues);
  388. if (parameterTypes.Length != parameterValues.Length)
  389. {
  390. return false;
  391. }
  392. for (int i = 0; i < parameterTypes.Length; ++i)
  393. {
  394. if (!parameterTypes[i].IsInstanceOfType(parameterValues[i]))
  395. {
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. private static bool AreAssignableFrom(ParameterInfo[] parameterInfos, params Type[] parameterTypes)
  402. {
  403. Assert.NotNull(parameterInfos);
  404. Assert.NotNull(parameterTypes);
  405. Type[] parameterInfoTypes = parameterInfos.Select<ParameterInfo, Type>((info) => info.ParameterType).ToArray();
  406. if (parameterInfoTypes.Length != parameterTypes.Length)
  407. {
  408. return false;
  409. }
  410. for (int i = 0; i < parameterInfoTypes.Length; ++i)
  411. {
  412. // Generic parameters are assumed to be assignable
  413. if (parameterInfoTypes[i].IsGenericParameter)
  414. {
  415. continue;
  416. }
  417. if (!parameterInfoTypes[i].IsAssignableFrom(parameterTypes[i]))
  418. {
  419. return false;
  420. }
  421. }
  422. return true;
  423. }
  424. private static bool AreAssignableFrom(ParameterInfo[] parameterInfos, params object[] parameterValues)
  425. {
  426. Assert.NotNull(parameterInfos);
  427. Assert.NotNull(parameterValues);
  428. Type[] parameterTypes = parameterInfos.Select<ParameterInfo, Type>((info) => info.ParameterType).ToArray();
  429. return AreAssignableFrom(parameterTypes, parameterValues);
  430. }
  431. private static ConstructorInfo FindConstructor(Type type, params object[] parameterValues)
  432. {
  433. Assert.NotNull(type);
  434. Assert.NotNull(parameterValues);
  435. return type.GetConstructors().FirstOrDefault((c) => AreAssignableFrom(c.GetParameters(), parameterValues));
  436. }
  437. private static MethodInfo FindMethod(Type type, string methodName, params object[] parameterValues)
  438. {
  439. Assert.NotNull(type);
  440. Assert.False(String.IsNullOrWhiteSpace(methodName));
  441. Assert.NotNull(parameterValues);
  442. return type.GetMethods().FirstOrDefault((m) => String.Equals(m.Name, methodName, StringComparison.Ordinal) && AreAssignableFrom(m.GetParameters(), parameterValues));
  443. }
  444. }
  445. }