Fixing bug in finding constructor by parameter values when a value is null

This commit is contained in:
Chris Cameron
2017-10-02 13:39:52 -04:00
parent f571807137
commit b2836e1b09

View File

@@ -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);
}
/// <summary>
/// Returns true if the parameters match the constructor parameters.
/// </summary>
/// <param name="constructor"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static bool MatchesConstructorParameters(ConstructorInfo constructor, IEnumerable<object> 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);
}
/// <summary>
/// Returns true if the parameters match the method parameters.
/// </summary>