mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
refactor: Tidying, removing duplicate/unused code, micro-optimization
This commit is contained in:
@@ -8,7 +8,6 @@ using Crestron.SimplSharp.CrestronIO;
|
|||||||
using Crestron.SimplSharp.Reflection;
|
using Crestron.SimplSharp.Reflection;
|
||||||
using Activator = Crestron.SimplSharp.Reflection.Activator;
|
using Activator = Crestron.SimplSharp.Reflection.Activator;
|
||||||
#else
|
#else
|
||||||
using System.IO;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.Extensions.DependencyModel;
|
using Microsoft.Extensions.DependencyModel;
|
||||||
using System.Runtime.Loader;
|
using System.Runtime.Loader;
|
||||||
@@ -33,18 +32,11 @@ namespace ICD.Common.Utils
|
|||||||
if (parameters == null)
|
if (parameters == null)
|
||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
|
|
||||||
#if SIMPLSHARP
|
return MatchesMethodParameters(constructor, parameters);
|
||||||
IEnumerable<CType>
|
|
||||||
#else
|
|
||||||
IEnumerable<Type>
|
|
||||||
#endif
|
|
||||||
parameterTypes = constructor.GetParameters().Select(p => p.ParameterType);
|
|
||||||
|
|
||||||
return ParametersMatchTypes(parameterTypes, parameters);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if the parameters match the method parameters.
|
/// Returns true if the parameter values match the method signature parameters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="method"></param>
|
/// <param name="method"></param>
|
||||||
/// <param name="parameters"></param>
|
/// <param name="parameters"></param>
|
||||||
@@ -78,14 +70,7 @@ namespace ICD.Common.Utils
|
|||||||
if (property == null)
|
if (property == null)
|
||||||
throw new ArgumentNullException("property");
|
throw new ArgumentNullException("property");
|
||||||
|
|
||||||
#if SIMPLSHARP
|
return ParameterMatchesType(property.PropertyType, parameter);
|
||||||
CType
|
|
||||||
#else
|
|
||||||
Type
|
|
||||||
#endif
|
|
||||||
propertyType = property.PropertyType;
|
|
||||||
|
|
||||||
return ParametersMatchTypes(new[] {propertyType}, new[] {parameter});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -97,7 +82,7 @@ namespace ICD.Common.Utils
|
|||||||
private static bool ParametersMatchTypes(
|
private static bool ParametersMatchTypes(
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
IEnumerable<CType>
|
IEnumerable<CType>
|
||||||
#else
|
#else
|
||||||
IEnumerable<Type>
|
IEnumerable<Type>
|
||||||
#endif
|
#endif
|
||||||
types, IEnumerable<object> parameters)
|
types, IEnumerable<object> parameters)
|
||||||
@@ -109,21 +94,21 @@ namespace ICD.Common.Utils
|
|||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
|
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
CType[]
|
IList<CType>
|
||||||
#else
|
#else
|
||||||
Type[]
|
IList<Type>
|
||||||
#endif
|
#endif
|
||||||
typesArray = types as
|
typesArray = types as
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
CType[]
|
IList<CType>
|
||||||
#else
|
#else
|
||||||
Type[]
|
IList<Type>
|
||||||
#endif
|
#endif
|
||||||
?? types.ToArray();
|
?? types.ToArray();
|
||||||
|
|
||||||
object[] parametersArray = parameters as object[] ?? parameters.ToArray();
|
IList<object> parametersArray = parameters as IList<object> ?? parameters.ToArray();
|
||||||
|
|
||||||
if (parametersArray.Length != typesArray.Length)
|
if (parametersArray.Count != typesArray.Count)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Compares each pair of items in the two arrays.
|
// Compares each pair of items in the two arrays.
|
||||||
@@ -175,26 +160,6 @@ namespace ICD.Common.Utils
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the given type has a public parameterless constructor.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Platform independant delegate instantiation.
|
/// Platform independant delegate instantiation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -213,6 +178,18 @@ namespace ICD.Common.Utils
|
|||||||
.CreateDelegate(type, firstArgument, method);
|
.CreateDelegate(type, firstArgument, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of the given type, calling the default constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T CreateInstance<T>(Type type)
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
throw new ArgumentNullException("type");
|
||||||
|
|
||||||
|
return (T)CreateInstance(type);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the given type, calling the default constructor.
|
/// Creates an instance of the given type, calling the default constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -238,18 +215,11 @@ namespace ICD.Common.Utils
|
|||||||
if (parameters == null)
|
if (parameters == null)
|
||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
|
|
||||||
ConstructorInfo constructor =
|
ConstructorInfo constructor = GetConstructor(type, parameters);
|
||||||
type
|
|
||||||
#if SIMPLSHARP
|
|
||||||
.GetCType()
|
|
||||||
#endif
|
|
||||||
.GetConstructors()
|
|
||||||
.FirstOrDefault(c => MatchesConstructorParameters(c, parameters));
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (constructor != null)
|
return constructor.Invoke(parameters);
|
||||||
return constructor.Invoke(parameters);
|
|
||||||
}
|
}
|
||||||
catch (TypeLoadException e)
|
catch (TypeLoadException e)
|
||||||
{
|
{
|
||||||
@@ -259,46 +229,29 @@ namespace ICD.Common.Utils
|
|||||||
{
|
{
|
||||||
throw e.GetBaseException();
|
throw e.GetBaseException();
|
||||||
}
|
}
|
||||||
|
|
||||||
string message = string.Format("Unable to find constructor for {0}", type.Name);
|
|
||||||
throw new InvalidOperationException(message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the given type, calling the default constructor.
|
/// Gets the constructor matching the given parameters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="parameters"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T CreateInstance<T>(Type type)
|
public static ConstructorInfo GetConstructor(Type type, params object[] parameters)
|
||||||
{
|
{
|
||||||
if (type == null)
|
if (type == null)
|
||||||
throw new ArgumentNullException("type");
|
throw new ArgumentNullException("type");
|
||||||
|
|
||||||
if (!type.IsAssignableTo(typeof(T)))
|
if (parameters == null)
|
||||||
throw new InvalidOperationException("Type is not assignable to T");
|
throw new ArgumentNullException("parameters");
|
||||||
|
|
||||||
return (T)CreateInstance(type);
|
return
|
||||||
}
|
type
|
||||||
|
#if SIMPLSHARP
|
||||||
/// <summary>
|
.GetCType()
|
||||||
/// Gets the custom attributes added to the given assembly.
|
#endif
|
||||||
/// </summary>
|
.GetConstructors()
|
||||||
/// <typeparam name="T"></typeparam>
|
.First(c => MatchesConstructorParameters(c, parameters));
|
||||||
/// <param name="assembly"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<T> GetCustomAttributes<T>(Assembly assembly)
|
|
||||||
where T : Attribute
|
|
||||||
{
|
|
||||||
if (assembly == null)
|
|
||||||
throw new ArgumentNullException("assembly");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return assembly.GetCustomAttributes<T>();
|
|
||||||
}
|
|
||||||
catch (FileNotFoundException)
|
|
||||||
{
|
|
||||||
return Enumerable.Empty<T>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -340,39 +293,6 @@ namespace ICD.Common.Utils
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the corresponding property info on the given type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type"></param>
|
|
||||||
/// <param name="property"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Changes the given value to the given type.
|
/// Changes the given value to the given type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user