refactor: Simplifying reflection code, potential micro-optimization

This commit is contained in:
Chris Cameron
2019-11-14 11:24:10 -05:00
parent de00acbf79
commit f61305856f

View File

@@ -51,13 +51,7 @@ namespace ICD.Common.Utils
if (parameters == null) if (parameters == null)
throw new ArgumentNullException("parameters"); throw new ArgumentNullException("parameters");
#if SIMPLSHARP IEnumerable<Type> parameterTypes = method.GetParameters().Select(p => (Type)p.ParameterType);
IEnumerable<CType>
#else
IEnumerable<Type>
#endif
parameterTypes = method.GetParameters().Select(p => p.ParameterType);
return ParametersMatchTypes(parameterTypes, parameters); return ParametersMatchTypes(parameterTypes, parameters);
} }
@@ -81,13 +75,7 @@ namespace ICD.Common.Utils
/// <param name="types"></param> /// <param name="types"></param>
/// <param name="parameters"></param> /// <param name="parameters"></param>
/// <returns></returns> /// <returns></returns>
private static bool ParametersMatchTypes( private static bool ParametersMatchTypes(IEnumerable<Type> types, IEnumerable<object> parameters)
#if SIMPLSHARP
IEnumerable<CType>
#else
IEnumerable<Type>
#endif
types, IEnumerable<object> parameters)
{ {
if (types == null) if (types == null)
throw new ArgumentNullException("types"); throw new ArgumentNullException("types");
@@ -95,27 +83,11 @@ namespace ICD.Common.Utils
if (parameters == null) if (parameters == null)
throw new ArgumentNullException("parameters"); throw new ArgumentNullException("parameters");
return types
#if SIMPLSHARP #if SIMPLSHARP
IList<CType> .Cast<object>()
#else
IList<Type>
#endif #endif
typesArray = types as .SequenceEqual(parameters, (a, b) => ParameterMatchesType((Type)a, b));
#if SIMPLSHARP
IList<CType>
#else
IList<Type>
#endif
?? types.ToArray();
IList<object> parametersArray = parameters as IList<object> ?? parameters.ToArray();
if (parametersArray.Count != typesArray.Count)
return false;
// Compares each pair of items in the two arrays.
return !parametersArray.Where((t, index) => !ParameterMatchesType(typesArray[index], t))
.Any();
} }
/// <summary> /// <summary>
@@ -164,7 +136,7 @@ namespace ICD.Common.Utils
} }
/// <summary> /// <summary>
/// Platform independant delegate instantiation. /// Platform independent delegate instantiation.
/// </summary> /// </summary>
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="firstArgument"></param> /// <param name="firstArgument"></param>