From d3f1fe3425ef8f87f48a8ad19f589a404214b934 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Sun, 9 Sep 2018 14:14:20 -0400 Subject: [PATCH] refactor: Tidying, removing duplicate/unused code, micro-optimization --- ICD.Common.Utils/ReflectionUtils.cs | 156 +++++++--------------------- 1 file changed, 38 insertions(+), 118 deletions(-) diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 5e284fb..314cb5f 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -8,7 +8,6 @@ using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.Reflection; using Activator = Crestron.SimplSharp.Reflection.Activator; #else -using System.IO; using System.Reflection; using Microsoft.Extensions.DependencyModel; using System.Runtime.Loader; @@ -33,18 +32,11 @@ namespace ICD.Common.Utils if (parameters == null) throw new ArgumentNullException("parameters"); -#if SIMPLSHARP - IEnumerable -#else - IEnumerable -#endif - parameterTypes = constructor.GetParameters().Select(p => p.ParameterType); - - return ParametersMatchTypes(parameterTypes, parameters); + return MatchesMethodParameters(constructor, parameters); } /// - /// Returns true if the parameters match the method parameters. + /// Returns true if the parameter values match the method signature parameters. /// /// /// @@ -78,14 +70,7 @@ namespace ICD.Common.Utils if (property == null) throw new ArgumentNullException("property"); -#if SIMPLSHARP - CType -#else - Type -#endif - propertyType = property.PropertyType; - - return ParametersMatchTypes(new[] {propertyType}, new[] {parameter}); + return ParameterMatchesType(property.PropertyType, parameter); } /// @@ -97,7 +82,7 @@ namespace ICD.Common.Utils private static bool ParametersMatchTypes( #if SIMPLSHARP IEnumerable -#else +#else IEnumerable #endif types, IEnumerable parameters) @@ -109,21 +94,21 @@ namespace ICD.Common.Utils throw new ArgumentNullException("parameters"); #if SIMPLSHARP - CType[] + IList #else - Type[] + IList #endif typesArray = types as #if SIMPLSHARP - CType[] + IList #else - Type[] + IList #endif - ?? types.ToArray(); + ?? types.ToArray(); - object[] parametersArray = parameters as object[] ?? parameters.ToArray(); + IList parametersArray = parameters as IList ?? parameters.ToArray(); - if (parametersArray.Length != typesArray.Length) + if (parametersArray.Count != typesArray.Count) return false; // Compares each pair of items in the two arrays. @@ -175,26 +160,6 @@ namespace ICD.Common.Utils : null; } - /// - /// Returns true if the given type has a public parameterless constructor. - /// - /// - /// - public static bool HasPublicParameterlessConstructor(Type type) - { - if (type == null) - throw new ArgumentNullException("type"); - - const BindingFlags binding = BindingFlags.Instance | BindingFlags.Public; - -#if SIMPLSHARP - return ((CType)type).GetConstructor(binding, null, new CType[0], null) -#else - return type.GetConstructor(binding, null, new Type[0], null) -#endif - != null; - } - /// /// Platform independant delegate instantiation. /// @@ -213,6 +178,18 @@ namespace ICD.Common.Utils .CreateDelegate(type, firstArgument, method); } + /// + /// Creates an instance of the given type, calling the default constructor. + /// + /// + public static T CreateInstance(Type type) + { + if (type == null) + throw new ArgumentNullException("type"); + + return (T)CreateInstance(type); + } + /// /// Creates an instance of the given type, calling the default constructor. /// @@ -238,18 +215,11 @@ namespace ICD.Common.Utils if (parameters == null) throw new ArgumentNullException("parameters"); - ConstructorInfo constructor = - type -#if SIMPLSHARP - .GetCType() -#endif - .GetConstructors() - .FirstOrDefault(c => MatchesConstructorParameters(c, parameters)); + ConstructorInfo constructor = GetConstructor(type, parameters); try { - if (constructor != null) - return constructor.Invoke(parameters); + return constructor.Invoke(parameters); } catch (TypeLoadException e) { @@ -259,46 +229,29 @@ namespace ICD.Common.Utils { throw e.GetBaseException(); } - - string message = string.Format("Unable to find constructor for {0}", type.Name); - throw new InvalidOperationException(message); } /// - /// Creates an instance of the given type, calling the default constructor. + /// Gets the constructor matching the given parameters. /// + /// + /// /// - public static T CreateInstance(Type type) + public static ConstructorInfo GetConstructor(Type type, params object[] parameters) { if (type == null) throw new ArgumentNullException("type"); - if (!type.IsAssignableTo(typeof(T))) - throw new InvalidOperationException("Type is not assignable to T"); + if (parameters == null) + throw new ArgumentNullException("parameters"); - return (T)CreateInstance(type); - } - - /// - /// Gets the custom attributes added to the given assembly. - /// - /// - /// - /// - public static IEnumerable GetCustomAttributes(Assembly assembly) - where T : Attribute - { - if (assembly == null) - throw new ArgumentNullException("assembly"); - - try - { - return assembly.GetCustomAttributes(); - } - catch (FileNotFoundException) - { - return Enumerable.Empty(); - } + return + type +#if SIMPLSHARP + .GetCType() +#endif + .GetConstructors() + .First(c => MatchesConstructorParameters(c, parameters)); } /// @@ -340,39 +293,6 @@ namespace ICD.Common.Utils #endif } - /// - /// Finds the corresponding property info on the given type. - /// - /// - /// - /// - public static PropertyInfo GetImplementation(Type type, PropertyInfo property) - { - if (type == null) - throw new ArgumentNullException("type"); - - if (property == null) - throw new ArgumentNullException("property"); - - if (type.IsInterface) - throw new InvalidOperationException("Type must not be an interface"); - - property = type -#if SIMPLSHARP - .GetCType() -#else - .GetTypeInfo() -#endif - .GetProperty(property.Name, property.PropertyType); - - if (property == null) - return null; - - return property.DeclaringType == type - ? property - : GetImplementation(property.DeclaringType, property); - } - /// /// Changes the given value to the given type. ///