using System; using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; #if SIMPLSHARP 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; using Activator = System.Activator; #endif namespace ICD.Common.Utils { public static class ReflectionUtils { /// /// Instantiates the given type using the constructor matching the given values. /// /// /// /// [PublicAPI] public static object Instantiate(Type type, params object[] values) { if (type == null) throw new ArgumentNullException("type"); #if SIMPLSHARP CType[] types = values.Select(v => (CType)v.GetType()) .ToArray(); ConstructorInfo constructor = ((CType)type).GetConstructor(types); #else Type[] types = values.Select(v => v.GetType()) .ToArray(); ConstructorInfo constructor = type.GetTypeInfo().GetConstructor(types); #endif if (constructor != null) return constructor.Invoke(values); string message = string.Format("Unable to find constructor for {0}", type.Name); throw new InvalidOperationException(message); } /// /// Returns true if the parameters match the method parameters. /// /// /// /// public static bool MatchesMethodParameters(MethodBase method, IEnumerable parameters) { if (method == null) throw new ArgumentNullException("method"); if (parameters == null) throw new ArgumentNullException("parameters"); #if SIMPLSHARP CType[] methodTypes #else Type[] methodTypes #endif = method.GetParameters().Select(p => p.ParameterType).ToArray(); return ParametersMatchTypes(methodTypes, parameters); } /// /// Returns true if the if the parameter match the property parameter. /// /// /// /// public static bool MatchesPropertyParameter(PropertyInfo property, object parameter) { if (property == null) throw new ArgumentNullException("property"); #if SIMPLSHARP CType propertyType #else Type propertyType #endif = property.PropertyType; return ParametersMatchTypes(new[] {propertyType}, new[] {parameter}); } /// /// Returns true if the parameters array is compatible with the given property types array. /// /// /// /// #if SIMPLSHARP private static bool ParametersMatchTypes(IEnumerable types, IEnumerable parameters) { if (types == null) throw new ArgumentNullException("types"); CType[] typesArray = types as CType[] ?? types.ToArray(); #else private static bool ParametersMatchTypes(IEnumerable types, IEnumerable parameters) { if (types == null) throw new ArgumentNullException("types"); Type[] typesArray = types as Type[] ?? types.ToArray(); #endif if (parameters == null) throw new ArgumentNullException("parameters"); object[] parametersArray = parameters as object[] ?? parameters.ToArray(); if (parametersArray.Length != typesArray.Length) return false; // Compares each pair of items in the two arrays. return !parametersArray.Where((t, index) => !ParameterMatchesType(typesArray[index], t)).Any(); } /// /// Returns true if the parameter can be assigned to the given type. /// /// /// /// #if SIMPLSHARP private static bool ParameterMatchesType(CType type, object parameter) { if (type == null) throw new ArgumentNullException("type"); // Can the parameter be assigned a null value? if (parameter == null) return (type.IsClass || !type.IsValueType || Nullable.GetUnderlyingType(type) != null); return type.IsInstanceOfType(parameter); } #else private static bool ParameterMatchesType(Type type, object parameter) { if (type == null) throw new ArgumentNullException("type"); TypeInfo info = type.GetTypeInfo(); // Can the parameter be assigned a null value? if (parameter == null) return (info.IsClass || !info.IsValueType || Nullable.GetUnderlyingType(type) != null); return info.IsInstanceOfType(parameter); } #endif /// /// Same as doing default(Type). /// /// /// #if SIMPLSHARP public static object GetDefaultValue(CType type) #else public static object GetDefaultValue(Type type) #endif { if (type == null) throw new ArgumentNullException("type"); return type #if !SIMPLSHARP .GetTypeInfo() #endif .IsValueType ? Activator.CreateInstance(type) : null; } /// /// Creates an instance of the given type, calling the default constructor. /// /// /// public static T CreateInstance() where T : new() { return Activator.CreateInstance(); } /// /// 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"); #if SIMPLSHARP return assembly.GetCustomAttributes(typeof(T), false); #else return assembly.GetCustomAttributes(); #endif } /// /// Loads the assembly at the given path. /// /// /// public static Assembly LoadAssemblyFromPath(string path) { #if SIMPLSHARP return Assembly.LoadFrom(path); #else string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(path); bool inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase)); bool inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase)); return inCompileLibraries || inRuntimeLibraries ? Assembly.Load(new AssemblyName(fileNameWithOutExtension)) : AssemblyLoadContext.Default.LoadFromAssemblyPath(path); #endif } } }