Ejemplo n.º 1
0
        /// <summary>
        /// Resolves the specified abstract type as a concrete instance.
        /// </summary>
        /// <param name="type">The abstract type to resolve.</param>
        /// <param name="name">An optional unique identifier for the abstract type.</param>
        /// <param name="parameters">An array of constructor parameters for initialization.</param>
        /// <exception cref="TypeLoadException">Thrown if the type cannot be found in the map.</exception>
        /// <returns>The object instance.</returns>
        public object Resolve(Type type, string name, params object[] parameters)
        {
            //Repackage invalid name as parameter
            if (name != null && !ContainsKey(type, name) && ContainsKey(type, null))
            {
                var paras = new List <object> {
                    name
                };
                if (parameters != null)
                {
                    paras.AddRange(parameters);
                }
                parameters = paras.ToArray();
                return(Resolve(type, null, parameters));
            }

            var initer = GetTypeLoader(type, name);

            if (initer.Type == null)
            {
                // If the type can't be initialized, return null
                if (!type.GetTypeInfo().DeclaredConstructors.Any())
                {
                    return(null);
                }

                // If the type is not registered at all, provide an initializer anyways
                initer = new TypeLoader(type);
                return(initer.Load(parameters));
            }

            // Add the loader to the items cache to persist singletons
            var retval = initer.Load(parameters);

            _items[new NamedType(type, name)] = initer;
            return(retval);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Adds an entry with the specified abstract type and class type to the collection.
 /// </summary>
 /// <param name="name">A unique identifier for the abstract type.</param>
 /// <param name="abstractType">The type of the abstract to associate with the class type.</param>
 /// <param name="typeLoader">The type loader to associate with the abstract type.</param>
 public void Add(string name, Type abstractType, TypeLoader typeLoader)
 {
     CheckEntry(ref name, abstractType, typeLoader.Type);
     _items.Add(new NamedType(abstractType, name), typeLoader);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Registers the specified key type and object for <see cref="Resolve"/>.
 /// </summary>
 /// <param name="keyType">The key type to associate with the value type.</param>
 /// <param name="mappedObject">The type of the class to associate with the key type.</param>
 /// <param name="namedInstance">An optional unique identifier for the key type.</param>
 public void Register(Type keyType, object mappedObject, string namedInstance)
 {
     this[keyType, namedInstance] = new TypeLoader(mappedObject);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Registers the specified key type and class type for <see cref="Resolve"/>.
 /// </summary>
 /// <param name="keyType">The key type to associate with the value type.</param>
 /// <param name="namedInstance">An optional unique identifier for the key type.</param>
 /// <param name="nativeType">The type of the class to associate with the key type.</param>
 /// <param name="initialization">A method that initializes the object.</param>
 /// <param name="singletonInstance"><c>true</c> to create and cache the instance; otherwise <c>false</c> to create every time.</param>
 public void Register(Type keyType, Type nativeType, string namedInstance, Func <object[], object> initialization, bool singletonInstance)
 {
     this[keyType, namedInstance] = new TypeLoader(nativeType, singletonInstance, initialization);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Registers the specified key type and class type for <see cref="Resolve"/>.
 /// </summary>
 /// <param name="keyType">The key type to associate with the value type.</param>
 /// <param name="mappedType">The type of the class to associate with the key type.</param>
 /// <param name="namedInstance">An optional unique identifier for the key type.</param>
 public void Register(Type keyType, Type mappedType, string namedInstance)
 {
     this[keyType, namedInstance] = new TypeLoader(mappedType);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets or sets the class type associated with the specified interface type.
 /// </summary>
 /// <param name="abstractType">The interface type associated with the class type to get or set.</param>
 /// <returns>The class type associated with the <paramref name="abstractType"/>.</returns>
 public Type this[Type abstractType]
 {
     get { return(this[abstractType, null].Type); }
     set { this[abstractType, null] = new TypeLoader(value); }
 }