Example #1
0
            public object Resolve(Type serviceType)
            {
                if (_scopeObjects.TryGetValue(serviceType, out var obj))
                {
                    return(obj);
                }

                using var scope = new ResolveLoopScope();
                obj             = ResolveHelper.Resolve(this, serviceType, out Lifetime lifetime);

                if (lifetime == Lifetime.Scoped)
                {
                    _scopeObjects.TryAdd(serviceType, obj);
                }

                if (obj is IDisposable dispObj)
                {
                    if (lifetime != Lifetime.Singleton)
                    {
                        _dispObjects.Add(dispObj);
                    }
                    else if (_root is Container container)
                    {
                        container.TryAddDisposableObject(dispObj);
                    }
                }

                return(obj);
            }
Example #2
0
        /// <summary>
        /// 解析出类型 <paramref name="serviceType"/> 的实例。
        /// </summary>
        /// <param name="serviceType">服务类型。</param>
        /// <returns>类型的实例对象。如果没有注册,则为 null。</returns>
        public object Resolve(Type serviceType)
        {
            using var scope = new ResolveLoopScope();
            var obj = ResolveHelper.Resolve(this, serviceType, out Lifetime lifetime);

            if (lifetime == Lifetime.Singleton && obj is IDisposable dispObj)
            {
                TryAddDisposableObject(dispObj);
            }

            return(obj);
        }