Beispiel #1
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static object TryGet(Type serviceType, string name = null)
        {
            Type service = serviceType;

            if (Bindings.ContainsKey(service))
            {
                IBindingInfo binding = Resolve(serviceType, name);

                if (binding != null)
                {
                    try
                    {
                        object instance = binding.GetInstance();

                        if (instance != null)
                        {
                            return(instance);
                        }
                    }
                    catch
                    {
                        return(null);
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <typeparam name="TType">The type of the type.</typeparam>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static TType TryGet <TType>(string name = null)
        {
            Type service = typeof(TType);

            if (Bindings.ContainsKey(service))
            {
                IBindingInfo binding = Resolve <TType>(name);

                if (binding != null)
                {
                    try
                    {
                        object instance = binding.GetInstance();

                        if (instance != null)
                        {
                            return((TType)instance);
                        }
                    }
                    catch
                    {
                        return(default(TType));
                    }
                }
            }

            return(default(TType));
        }
Beispiel #3
0
        /// <summary>
        /// The get.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="name">The name.</param>
        /// <returns>
        /// Resolved Type Reference.
        /// </returns>
        public static object Get(Type serviceType, string name = null)
        {
            Type service = serviceType;

            if (!Bindings.ContainsKey(service))
            {
                throw new ActivationException(
                          string.Format("No registration for type {0} could be found.", service.FullName));
            }

            IBindingInfo binding = Resolve(service, name);

            if (binding == null)
            {
                throw new ActivationException(
                          string.Format("No registration for type {0} could be found.", service.FullName));
            }

            try
            {
                object instance = binding.GetInstance();

                if (instance == null)
                {
                    throw new ActivationException(
                              string.Format("The registered delegate for type {0} returned null.", service.FullName));
                }

                return(instance);
            }
            catch (Exception exception)
            {
                throw new ActivationException("Cannot Resolve type " + service.FullName, exception);
            }
        }