public static object CreateAndCompose(Type type, params object[] nonComposedParams) { var nonComposedParamsList = nonComposedParams.ToList(); var constructor = type.GetConstructors().FirstOrDefault(x => x.IsPublic); if (constructor == null) { throw new NullReferenceException($"Type {type.FullName} contains no public constructor"); } var constructorArgs = new List <object>(); var i = 0; foreach (var prmInfo in constructor.GetParameters()) { var prm = nonComposedParamsList.FirstOrDefault(x => x.GetType() == prmInfo.ParameterType); if (prm == null) { foreach (var p in nonComposedParams) { var pt = p.GetType(); var interfaces = pt.GetInterfaces(); if (interfaces.Any(x => x == prmInfo.ParameterType)) { prm = p; break; } } } if (prm != null) { nonComposedParamsList.Remove(prm); constructorArgs.Add(prm); } else if (prmInfo.ParameterType.IsInterface) { var singletonType = ExportedTypes.FirstOrDefault(x => x.ExposedType == prmInfo.ParameterType)?.ActualType; if (singletonType == null) { throw new NullReferenceException($"No type {prmInfo.ParameterType.FullName} could be matched with any exported type"); } var singleton = GetSingleton(singletonType); constructorArgs.Add(singleton); } else { throw new Exception($"Found no matching argument for parameter {prmInfo.Name} in constructor for type {type.FullName}"); } } var o = constructor.Invoke(constructorArgs.ToArray()); Compose(o); return(o); }
/// <summary> /// Tries to find/identify a <see cref="Type"/> by its name /// </summary> /// <param name="typeName">The name of the <see cref="Type"/></param> /// <returns>The <see cref="Type"/> that is defined by the parameter <paramref name="typeName"/></returns> /// <exception cref="ArgumentNullException"> /// The <paramref name="typeName"/> parameter is null /// </exception> /// <exception cref="ArgumentException"> /// The <paramref name="typeName"/> parameter is an empty string /// </exception> public static Type GetTypeFromName(string typeName) { if (typeName == null) { throw new ArgumentNullException(nameof(typeName)); } if (typeName.Length == 0) { throw new ArgumentException("The parameter is an empty string", nameof(typeName)); } var result = ExportedTypes .FirstOrDefault(x => x.FullName != null && x.FullName.GetHashCode() == typeName.GetHashCode() && x.FullName == typeName); if (result != null) { return(result); } // Maybe the type is just a part of the type name... result = ExportedTypes.FirstOrDefault(x => x.FullName != null && x.Name.EndsWith(typeName)); if (result != null) { return(result); } // Else let us loop through all known assemblies and look for the type for (int i = 0; i < _assemblies.Count; i++) { var type = _assemblies[i].GetType(typeName); if (type != null) { return(type); } } // The last resort result = Type.GetType(typeName); if (result != null) { return(result); } Output.WriteLineError($"The type '{typeName}' defined by the parameter 'typeName' was not found"); return(null); }
public static object GetExportedTypeInstance(Type type) { var t = ExportedTypes.FirstOrDefault(x => x.ExposedType == type); if (t == null) { throw new NullReferenceException($"No type {type.FullName} has been exported"); } switch (t.Many) { case true: return(CreateAndCompose(t.ActualType)); default: return(GetSingleton(t.ActualType)); } }