using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace Jace.Util { public static class TypeExtensions { /// /// Get constructor for a given type matching with the parameter types provided. /// /// The type for witch a matching constructor must be found. /// The types of the parameters of the constructor. /// The matching constructor. public static ConstructorInfo GetConstructor(this Type type, Type[] parameters) { IEnumerable constructors = type.GetTypeInfo().DeclaredConstructors.Where(c => c.GetParameters().Length == parameters.Length); foreach (ConstructorInfo constructor in constructors) { bool parametersMatch = true; ParameterInfo[] constructorParameters = constructor.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (parameters[i] != constructorParameters[i].ParameterType) { parametersMatch = false; break; } } if (parametersMatch) return constructor; } throw new Exception("No constructor was found matching with the provided parameters."); } } }