示例#1
0
 /// <summary>
 /// Initializes a new instance of <see cref="AutoContainer"/>, using the
 /// <paramref name="instances"/> as its registered dependencies. These
 /// depenendencies will be resolvable by this instance of
 /// <see cref="AutoContainer"/> via any type that exactly one dependency
 /// equals, implements, or inherits from. This instance of <see cref="AutoContainer"/>
 /// will use the <see cref="IResolverConstructorSelector"/> specified by
 /// <paramref name="constructorSelector"/> internally to determine
 /// which constructor of an arbitrary type will be selected for invocation when
 /// <see cref="Get{T}"/> or <see cref="Get"/> methods are called.
 /// </summary>
 /// <param name="constructorSelector">
 /// An object that determines which constructor should be used when creating an instance of a type.
 /// If null, the value of the <see cref="DefaultResolverConstructorSelector"/> property is used.
 /// </param>
 /// <param name="instances">The objects to use as registered dependencies.</param>
 public AutoContainer(IResolverConstructorSelector constructorSelector, IEnumerable <object> instances)
     : this(
         (instances ?? new object[0]).Where(x => x != null).ToList(), // Filter out nulls and .ToList() it for fast enumeration.
         new ConcurrentDictionary <Type, Func <object> >(),
         constructorSelector ?? DefaultResolverConstructorSelector)
 {
 }
        public static ConstructorInfo GetConstructor(this IResolverConstructorSelector constructorSelector, Type type, IResolver resolver)
        {
            ConstructorInfo ctor;

            if (constructorSelector.TryGetConstructor(type, resolver, out ctor))
            {
                return(ctor);
            }

            throw new ResolveException("Unable to resolve type: " + type);
        }
示例#3
0
        private AutoContainer(
            IEnumerable <object> instances,
            ConcurrentDictionary <Type, Func <object> > bindings,
            IResolverConstructorSelector constructorSelector)
        {
            if (instances == null)
            {
                throw new ArgumentNullException("instances");
            }
            if (bindings == null)
            {
                throw new ArgumentNullException("bindings");
            }
            if (constructorSelector == null)
            {
                throw new ArgumentNullException("constructorSelector");
            }

            _instances           = instances;
            _bindings            = bindings;
            _constructorSelector = constructorSelector;
        }
示例#4
0
 /// <summary>
 /// Sets the default <see cref="IResolverConstructorSelector"/>. If the
 /// <see cref="DefaultResolverConstructorSelector"/> has been accessed, then calls to this method
 /// have no effect.
 /// </summary>
 /// <param name="resolverConstructorSelector">
 /// The value that <see cref="DefaultResolverConstructorSelector"/> property should return. Ignored
 /// if the <see cref="DefaultResolverConstructorSelector"/> property has previously been accessed.
 /// </param>
 public static void SetDefaultResolverConstructorSelector(IResolverConstructorSelector resolverConstructorSelector)
 {
     _defaultResolverConstructorSelector.Value = resolverConstructorSelector;
 }
        public static bool CanGetConstructor(this IResolverConstructorSelector constructorSelector, Type type, IResolver resolver)
        {
            ConstructorInfo dummy;

            return(constructorSelector.TryGetConstructor(type, resolver, out dummy));
        }