/// <summary> /// Creates a new instance /// </summary> /// <param name="descriptor"></param> /// <param name="provider"></param> /// <returns></returns> internal static object GetInstance(this ServiceDescriptor descriptor, IFactoryProvider provider) { ConstructorInfo item = descriptor.ImplementationType.GetConstructors().FirstOrDefault(o => o.IsPublic && !o.IsStatic); if (item != null) { List <object> args = new List <object>(); foreach (var attr in item.GetParameters()) { var attr_type = attr.ParameterType; args.Add(provider.Get(attr_type) ?? attr.DefaultValue); } return(args.Count > 0 ? Activator.CreateInstance(descriptor.ImplementationType, args.ToArray()) : Activator.CreateInstance(descriptor.ImplementationType)); } else { if (provider.ThrowNotExist) { throw new FactoryConfigurationException($"Implementation {descriptor.ImplementationType.Name} not have public constructors"); } else { return(default);
/// <summary> /// Gets builder from the provider by the specified factory identifier and builder identifier. /// <para> /// The type of the factory identifier represents the identifier of the factory collection. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> public static TBuilder GetBuilder <TBuilder, TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId) where TBuilder : IBuilder { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } IFactoryCollection <TFactoryId> collection = provider.Get <TFactoryId>(); var factory = collection.Get <IFactory <TBuilderId> >(factoryId); return(factory.Get <TBuilder>(builderId)); }