Ejemplo n.º 1
0
        /// <summary>
        /// 装配
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <param name="typeDescription"></param>
        /// <returns></returns>
        /// <exception cref="DependencyResolutionException"></exception>
        private object Resolve(Type classType, Type type, IComponentContext context, string typeDescription)
        {
            object obj = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                context.TryResolveKeyed(this.Name, type, out obj);
            }
            else
            {
                if (type.IsGenericEnumerableInterfaceType())
                {
                    var genericType = type.GenericTypeArguments[0];
                    if (genericType.FullName != null && genericType.FullName.StartsWith("System.Lazy`1"))
                    {
                        genericType = genericType.GenericTypeArguments[0];
                    }

                    context.TryResolveKeyed("`1System.Collections.Generic.IEnumerable`1" + genericType.FullName, type, out obj);
                }
                else
                {
                    context.TryResolve(type, out obj);
                }
            }

            if (obj == null && this.Required)
            {
                throw new DependencyResolutionException($"Autowire error,can not resolve class type:{classType.FullName},${typeDescription} name:{type.Name} "
                                                        + (!string.IsNullOrEmpty(this.Name) ? $",with key:[{this.Name}]" : ""));
            }

            return(obj);
        }
Ejemplo n.º 2
0
        public (bool Successful, object Value) Resolve(IComponentContext context, InjectResolverParam param)
        {
            var type = param.Type;
            var attr = param.Attribute;
            var key  = attr.Key;

            if (key != null)
            {
                if (context.TryResolveKeyed(key, type, out var value))
                {
                    return(true, value);
                }

                Assertion.IsTrue(!attr.Required, $"required inject type {type.Name} with key {key} not resolved.");
                return(false, null);
            }

            {
                if (context.TryResolve(type, out var value))
                {
                    return(true, value);
                }

                Assertion.IsTrue(!attr.Required, $"required inject type {type.Name} not resolved.");
                return(false, null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>Resolves a constructor parameter based on keyed service requirements.
        /// </summary>
        /// <param name="parameter">The specific parameter being resolved that is marked with this attribute.</param>
        /// <param name="context">The component context under which the parameter is being resolved.</param>
        /// <returns>The instance of the object that should be used for the parameter value.
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">Thrown if <paramref name="parameter"/> or <paramref name="context"/> is <see langword="null"/>.</exception>
        public override object ResolveParameter(ParameterInfo parameter, IComponentContext context)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var options = context.Resolve <IOptions>();
            var key     = "console";

            if (!string.IsNullOrEmpty(options.Csv))
            {
                key = "csv";
            }
            else if (options.Daemon)
            {
                key = "daemon";
            }

            object obj;

            context.TryResolveKeyed(key, parameter.ParameterType, out obj);
            return(obj);
        }
Ejemplo n.º 4
0
        public static TService ResolveKeyedOrAny <TService, TKey>(this IComponentContext instance, TKey key)
        {
            object service = null;

            return(instance.TryResolveKeyed(key, typeof(TService), out service)
                ? (TService)service
                : instance.Resolve <TService>());
        }
Ejemplo n.º 5
0
 private static bool TryResolveAtScope(IComponentContext scope, string key, Type serviceType, out object value)
 {
     if (scope != null)
     {
         return(key == null
             ? scope.TryResolve(serviceType, out value)
             : scope.TryResolveKeyed(key, serviceType, out value));
     }
     value = null;
     return(false);
 }
Ejemplo n.º 6
0
        public static TInterface ResolveClient <TInterface>(IComponentContext cxt, string ept, string providerName)
        {
            if (providerName == null)
            {
                providerName = string.Empty;
            }

            object result = null;

            cxt.TryResolveKeyed(ept + providerName, typeof(TInterface), out result); // Not an error if not resolved.
            return((TInterface)result);
        }
Ejemplo n.º 7
0
        private static object ResolveWithVersioning(ParameterInfo pi, IComponentContext ctx)
        {
            var    versionService = ctx.Resolve <IVersionService>();
            string version        = versionService.CurrentVersion ?? versionService.DefaultVersion;

            object result;

            if (ctx.TryResolveKeyed(version, pi.ParameterType, out result))
            {
                return(result);
            }
            return(ctx.Resolve(pi.ParameterType));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Resolves a constructor parameter based on keyed service requirements.
        /// </summary>
        /// <param name="parameter">The specific parameter being resolved that is marked with this attribute.</param>
        /// <param name="context">The component context under which the parameter is being resolved.</param>
        /// <returns>
        /// The instance of the object that should be used for the parameter value.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="parameter" /> or <paramref name="context" /> is <see langword="null" />.
        /// </exception>
        public override object?ResolveParameter(ParameterInfo parameter, IComponentContext context)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.TryResolveKeyed(Key, parameter.ParameterType, out var value);
            return(value);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Resolves a constructor parameter based on keyed service requirements.
        /// </summary>
        /// <param name="parameter">The specific parameter being resolved that is marked with this attribute.</param>
        /// <param name="context">The component context under which the parameter is being resolved.</param>
        /// <returns>
        /// The instance of the object that should be used for the parameter value.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="parameter" /> or <paramref name="context" /> is <see langword="null" />.
        /// </exception>
        public override object ResolveParameter(ParameterInfo parameter, IComponentContext context)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            object value;

            context.TryResolveKeyed(this.Key, parameter.ParameterType, out value);
            return(value);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 装配
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <param name="typeDescription"></param>
        /// <returns></returns>
        /// <exception cref="DependencyResolutionException"></exception>
        private object Resolve(Type classType, Type type, IComponentContext context, string typeDescription)
        {
            object obj = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                context.TryResolveKeyed(this.Name, type, out obj);
            }
            else
            {
                context.TryResolve(type, out obj);
            }

            if (obj == null && this.Required)
            {
                throw new DependencyResolutionException($"Autowire error,can not resolve class type:{classType.FullName},${typeDescription} name:{type.Name} "
                                                        + (!string.IsNullOrEmpty(this.Name) ? $",with key:[{this.Name}]" : ""));
            }

            return(obj);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Resolves a constructor parameter based on keyed service requirements.
 /// </summary>
 /// <param name="parameter">The specific parameter being resolved that is marked with this attribute.</param>
 /// <param name="context">The component context under which the parameter is being resolved.</param>
 /// <returns>
 /// The instance of the object that should be used for the parameter value.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="parameter" /> or <paramref name="context" /> is <see langword="null" />.
 /// </exception>
 public override object ResolveParameter(ParameterInfo parameter, IComponentContext context)
 {
     if (parameter == null)
     {
         throw new ArgumentNullException("parameter");
     }
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     object value;
     context.TryResolveKeyed(this.Key, parameter.ParameterType, out value);
     return value;
 }