/// <summary>
        /// 返回依赖注入服务的实现类型
        /// </summary>
        /// <returns></returns>
        public Type GetImplementationType()
        {
            if (ImplementationType != null)
            {
                return(ImplementationType);
            }
            else if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }
            else if (ImplementationDelegate != null)
            {
                return(GetDelegateReturnType());
            }

            throw new InvalidOperationException(string.Format("无法获取{0}的实现类型。", ServiceType.FullName));
        }
        public Type GetImplementType()
        {
            if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }

            if (ImplementationFactory != null)
            {
                return(ImplementationFactory.Method.DeclaringType);
            }

            if (ImplementType != null)
            {
                return(ImplementType);
            }

            return(ServiceType);
        }
Пример #3
0
 internal Type GetImplementationType()
 {
     if (ImplementationType != null)
     {
         return(ImplementationType);
     }
     if (ImplementationInstance != null)
     {
         return(ImplementationInstance.GetType());
     }
     if (ImplementationFactory != null)
     {
         Type[] typeArgs = ImplementationFactory.GetType().GenericTypeArguments;
         if (typeArgs.Length == 2)
         {
             return(typeArgs[1]);
         }
     }
     throw new ArgumentException(Resources.Ioc_ImplementationTypeNotFound.FormatWith(ServiceType));
 }
Пример #4
0
 /// <summary>
 /// 获取实例类型
 /// </summary>
 /// <returns></returns>
 internal Type GetImplementationType()
 {
     if (ImplementationType != null)
     {
         return(ImplementationType);
     }
     if (ImplementationInstance != null)
     {
         return(ImplementationInstance.GetType());
     }
     if (ImplementationFactory != null)
     {
         Type[] typeArgs = ImplementationFactory.GetType().GenericTypeArguments;
         if (typeArgs.Length == 2)
         {
             return(typeArgs[1]);
         }
     }
     throw new ArgumentException("类型\"{0}\" 的实现类型无法找到".FormatWith(ServiceType));
 }
        internal Type GetImplementationType()
        {
            if (ImplementationType != null)
            {
                return(ImplementationType);
            }
            else if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }
            else if (ImplementationFactory != null)
            {
                var typeArguments = ImplementationFactory.GetType().GenericTypeArguments;

                Debug.Assert(typeArguments.Length == 2);

                return(typeArguments[1]);
            }

            throw new ArgumentException(Resources.FormatNoImplementation(ServiceType));
        }
Пример #6
0
        internal Type GetImplementationType()
        {
            if (ImplementationType != null)
            {
                return(ImplementationType);
            }
            else if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }
            else if (ImplementationFactory != null)
            {
                Type[]? typeArguments = ImplementationFactory.GetType().GenericTypeArguments;

                Debug.Assert(typeArguments.Length == 2);

                return(typeArguments[1]);
            }

            Debug.Assert(false, "ImplementationType, ImplementationInstance or ImplementationFactory must be non null");
            return(null);
        }
        internal System.Type GetImplementationType()
        {
            if (ImplementationType != null)
            {
                return(ImplementationType);
            }
            else if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }
            else if (ImplementationFactory != null)
            {
                var typeArguments = ImplementationFactory.GetType().GenericTypeArguments;

                System.Diagnostics.Debug.Assert(typeArguments.Length == 2);

                return(typeArguments[1]);
            }

            System.Diagnostics.Debug.Assert(false, "ImplementationType, ImplementationInstance or ImplementationFactory must be non null");

            return(null);
        }
Пример #8
0
        /// <summary>
        /// 获得<see cref="ImplementationType"/>、<see cref="ImplementationInstance"/>、<see cref="ImplementationFactory"/>
        /// 的<see cref="Type"/>类型。
        /// </summary>
        /// <returns></returns>
        internal Type GetImplementationType()
        {
            if (ImplementationType != null)
            {
                return(ImplementationType);
            }
            if (ImplementationInstance != null)
            {
                return(ImplementationInstance.GetType());
            }
            if (ImplementationFactory != null)
            {
                var Typearguments = ImplementationFactory.GetType().GenericTypeArguments;
                if (Typearguments.Length != 2)
                {
                    throw new Exception("ImplementationFactory只能提供两个泛型");
                }

                return(Typearguments[1]);
            }

            return(null);
        }
Пример #9
0
        private void Validate()
        {
            // ServiceType must be specified.
            if (ServiceType == null)
            {
                throw new IoCException("No service type was specified.");
            }

            // Implementation type or implementation expression or implementation
            // instance must be specified, but no more than one of the above.
            var count = new object[]
            { ImplementationType, ImplementationFactory, ImplementationInstance }
            .Count(x => x != null);

            if (count == 0)
            {
                throw new IoCException
                          ($"No implementation was specified for type {ServiceType.FullName}.");
            }

            // Implementation type and implementation expression can not both be specified
            if (count > 1)
            {
                throw new IoCException
                          ("More than one implementation was specified for type " +
                          ServiceType.FullName);
            }

            var actualImplementationType = ImplementationType;

            if (ImplementationFactory != null)
            {
                if (ImplementationFactory.Body is UnaryExpression uex)
                {
                    if (uex.NodeType == ExpressionType.Convert)
                    {
                        actualImplementationType = uex.Operand.Type;
                    }
                }
                actualImplementationType = actualImplementationType ??
                                           ImplementationFactory.Body.Type;
            }

            if (ImplementationInstance != null)
            {
                actualImplementationType = ImplementationInstance.GetType();
            }

            // Implementation must not be a value type.
            if (actualImplementationType.IsValueType)
            {
                throw new IoCException
                          ($"Type {ServiceType.FullName} has been specified to be " +
                          $"implemented by type {actualImplementationType.FullName}, " +
                          $"which is a value type. Value types are not supported as " +
                          $"registered services.");
            }

            // Implementations by type must be concrete classes.
            if (ImplementationType != null && ImplementationType.IsAbstract)
            {
                throw new IoCException
                          ($"Type {ServiceType.FullName} has been specified to be " +
                          $"implemented by type {ImplementationType.FullName}, " +
                          $"which is an interface or an abstract base class. " +
                          $"Services implemented by type must be concrete classes.");
            }

            if (ServiceType.IsGenericTypeDefinition)
            {
                ValidateOpenGeneric();
            }
            else
            {
                ValidateClosedType(actualImplementationType);
            }
        }