From b2836e1b092948ed393073d77a6b1e7aaedc5657 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 2 Oct 2017 13:39:52 -0400 Subject: [PATCH] Fixing bug in finding constructor by parameter values when a value is null --- ICD.Common.Utils/ReflectionUtils.cs | 34 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 66d921a..bf422fb 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -31,15 +31,14 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); + ConstructorInfo constructor = #if SIMPLSHARP - CType[] types = values.Select(v => (CType)v.GetType()) - .ToArray(); - ConstructorInfo constructor = ((CType)type).GetConstructor(types); + ((CType)type) #else - Type[] types = values.Select(v => v.GetType()) - .ToArray(); - ConstructorInfo constructor = type.GetTypeInfo().GetConstructor(types); + type #endif + .GetConstructors() + .FirstOrDefault(c => MatchesConstructorParameters(c, values)); try { @@ -55,6 +54,29 @@ namespace ICD.Common.Utils throw new InvalidOperationException(message); } + /// + /// Returns true if the parameters match the constructor parameters. + /// + /// + /// + /// + public static bool MatchesConstructorParameters(ConstructorInfo constructor, IEnumerable parameters) + { + if (constructor == null) + throw new ArgumentNullException("constructor"); + + if (parameters == null) + throw new ArgumentNullException("parameters"); + +#if SIMPLSHARP + CType[] methodTypes +#else + Type[] methodTypes +#endif + = constructor.GetParameters().Select(p => p.ParameterType).ToArray(); + return ParametersMatchTypes(methodTypes, parameters); + } + /// /// Returns true if the parameters match the method parameters. ///