Ejemplo n.º 1
0
        /// <summary>
        /// 装配字段
        /// </summary>
        /// <param name="property"></param>
        /// <param name="context"></param>
        /// <param name="Parameters"></param>
        /// <param name="instance"></param>
        /// <param name="allowCircle"></param>
        /// <returns></returns>
        public object ResolveField(FieldInfo property, IComponentContext context, IEnumerable <Parameter> Parameters, object instance, bool allowCircle)
        {
            if (!allowCircle)
            {
                return(property == null ? null : Resolve(property.DeclaringType, property.FieldType, context, "field"));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }


            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, property.FieldType);
            }
            else
            {
                propertyService = new TypedService(property.FieldType);
            }

            if (Parameters != null && Parameters.Count() == 1)
            {
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }
                if (AutowiredParmeter.AutowiredChains.TryGetValue(property.FieldType.FullName, out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    AutowiredParmeter.Add(property.DeclaringType.FullName, instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                instanceTypeParameter.Add(property.DeclaringType.FullName, instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        internal object Resolve(IComponentContext context, object instance, Type classType, Type memberType, IEnumerable <Parameter> Parameters)
        {
            if ((typeof(IObjectFactory).IsAssignableFrom(memberType)))
            {
                return(context.Resolve <ObjectBeanFactory>().CreateAutowiredFactory(this, memberType, classType, instance, Parameters));
            }

            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, memberType);
            }
            else
            {
                propertyService = new TypedService(memberType);
            }

            // ReSharper disable once PossibleMultipleEnumeration
            if (Parameters != null && Parameters.Count() == 1)
            {
                // ReSharper disable once PossibleMultipleEnumeration
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }

                // ReSharper disable once AssignNullToNotNullAttribute
                if (AutowiredParmeter.TryGet(getAutowiredParmeterKey(memberType), out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    // ReSharper disable once PossibleNullReferenceException
                    AutowiredParmeter.TryAdd(getAutowiredParmeterKey(classType), instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                // ReSharper disable once PossibleNullReferenceException
                instanceTypeParameter.TryAdd(getAutowiredParmeterKey(classType), instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
 public static bool TryResolveKeyed(this IComponentContext context, object serviceKey, Type serviceType, out object instance)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     return(context.TryResolveService(new KeyedService(serviceKey, serviceType), NoParameters, out instance));
 }
Ejemplo n.º 4
0
        public static bool TryResolveService(this IComponentContext context, Service service, out object instance)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(context.TryResolveService(service, NoParameters, out instance));
        }
Ejemplo n.º 5
0
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                     .GetRuntimeProperties()
                     .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (propertyType.GetTypeInfo().IsValueType&& !propertyType.GetTypeInfo().IsEnum)
                {
                    continue;
                }

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (property.GetIndexParameters().Length != 0)
                {
                    continue;
                }

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                object propertyValue;
                var    propertyService       = new TypedService(propertyType);
                var    instanceTypeParameter = new NamedParameter(InstanceTypeNamedParameter, instanceType);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out propertyValue))
                {
                    property.SetValue(instance, propertyValue, null);
                }
            }
        }
Ejemplo n.º 6
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            if (_context.TryResolveService(GetService(key), out var result))
            {
                value = (TValue)result;
                return(true);
            }

            value = default !;
Ejemplo n.º 7
0
        /// <summary>
        /// Inject properties onto an instance, filtered by a property selector.
        /// </summary>
        /// <param name="context">The component context to resolve dependencies from.</param>
        /// <param name="instance">The instance to inject onto.</param>
        /// <param name="propertySelector">The property selector.</param>
        /// <param name="parameters">The set of parameters for the resolve that can be used to satisfy injectable properties.</param>
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector, IEnumerable <Parameter> parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var resolveParameters = parameters as Parameter[] ?? parameters.ToArray();

            var instanceType         = instance.GetType();
            var injectableProperties = InjectableProperties.GetOrAdd(instanceType, type => GetInjectableProperties(type).ToArray());

            for (var index = 0; index < injectableProperties.Length; index++)
            {
                var property = injectableProperties[index];

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                // SetMethod will be non-null if GetInjectableProperties included it.
                var setParameter  = property.SetMethod !.GetParameters()[0];
                var valueProvider = (Func <object?>?)null;
                var parameter     = resolveParameters.FirstOrDefault(p => p.CanSupplyValue(setParameter, context, out valueProvider));
                if (parameter != null)
                {
                    var setter = PropertySetters.GetOrAdd(property, MakeFastPropertySetter);
                    setter(instance, valueProvider !());
                    continue;
                }

                var propertyService       = new TypedService(property.PropertyType);
                var instanceTypeParameter = new NamedParameter(InstanceTypeNamedParameter, instanceType);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    var setter = PropertySetters.GetOrAdd(property, MakeFastPropertySetter);
                    setter(instance, propertyValue);
                }
            }
        }
Ejemplo n.º 8
0
        private static object ResolveServiceSafe(IComponentContext context, Service service, IEnumerable <Parameter> parameters)
        {
            object instance;
            var    successful = context.TryResolveService(service, parameters, out instance);

            if (!successful)
            {
                throw new ComponentNotRegisteredException(service);
            }
            return(instance);
        }
        public bool TryGetValue(TKey key, out TValue value)
        {
            if (context.TryResolveService(GetService(key), out object result))
            {
                value = (TValue)result;
                return(true);
            }

            value = default(TValue);
            return(false);
        }
Ejemplo n.º 10
0
        public static object ResolveOptionalService(this IComponentContext context, Service service, IEnumerable <Parameter> parameters)
        {
            object obj2;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            context.TryResolveService(service, parameters, out obj2);
            return(obj2);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Retrieve a service from the context, or null if the service is not
        /// registered.
        /// </summary>
        /// <param name="context">The context from which to resolve the service.</param>
        /// <param name="service">The service.</param>
        /// <param name="parameters">Parameters for the service.</param>
        /// <returns>
        /// The component instance that provides the service, or null.
        /// </returns>
        /// <exception cref="DependencyResolutionException"/>
        public static object ResolveOptionalService(this IComponentContext context, Service service, IEnumerable <Parameter> parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            object instance;

            context.TryResolveService(service, parameters, out instance);
            return(instance);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Eagerly enumerate the services keyed with <see cref="Key_DoNotSerialize"/> that will not be serialized.
 /// </summary>
 /// <remarks>
 /// Services marked with <see cref="Key_DoNotSerialize"/> will not serialize their dependencies either.
 /// </remarks>
 private static IEnumerable <object> DoNotSerialize(IComponentContext context, IEnumerable <Parameter> parameters)
 {
     foreach (var registration in context.ComponentRegistry.Registrations)
     {
         foreach (var service in registration.Services)
         {
             var keyed = service as KeyedService;
             if (keyed != null)
             {
                 if (keyed.ServiceKey == Key_DoNotSerialize)
                 {
                     object instance;
                     if (context.TryResolveService(service, parameters, out instance))
                     {
                         yield return(instance);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Retrieve a service from the context.
        /// </summary>
        /// <param name="context">The context from which to resolve the service.</param>
        /// <param name="parameters">Parameters for the service.</param>
        /// <param name="service">The service to resolve.</param>
        /// <returns>
        /// The component instance that provides the service.
        /// </returns>
        /// <exception cref="ComponentNotRegisteredException"/>
        /// <exception cref="DependencyResolutionException"/>
        public static object ResolveService(this IComponentContext context, Service service, IEnumerable <Parameter> parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            object instance;
            var    successful = context.TryResolveService(service, parameters, out instance);

            if (!successful)
            {
                throw new ComponentNotRegisteredException(service);
            }
            return(instance);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 装配字段
        /// </summary>
        /// <param name="property"></param>
        /// <param name="context"></param>
        /// <param name="Parameters"></param>
        /// <param name="instance"></param>
        /// <param name="allowCircle"></param>
        /// <returns></returns>
        public object ResolveField(FieldInfo property, IComponentContext context, IEnumerable <Parameter> Parameters, object instance, bool allowCircle)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }
            if (!allowCircle)
            {
                return(Resolve(property.DeclaringType, property.FieldType, context, "field"));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }


            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, property.FieldType);
            }
            else
            {
                propertyService = new TypedService(property.FieldType);
            }

            // ReSharper disable once PossibleMultipleEnumeration
            if (Parameters != null && Parameters.Count() == 1)
            {
                // ReSharper disable once PossibleMultipleEnumeration
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }
                // ReSharper disable once AssignNullToNotNullAttribute
                if (AutowiredParmeter.TryGet(getAutowiredParmeterKey(property.FieldType), out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    // ReSharper disable once PossibleNullReferenceException
                    AutowiredParmeter.TryAdd(getAutowiredParmeterKey(property.DeclaringType), instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                // ReSharper disable once PossibleNullReferenceException
                instanceTypeParameter.TryAdd(getAutowiredParmeterKey(property.DeclaringType), instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }
            return(null);
        }
Ejemplo n.º 15
0
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector, IEnumerable <Parameter> parameters)
        {
            //判断instance是否是代理类型,如果是代理类型,则取Target
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            var unproxyedInstance     = UnwrapProxy(instance);
            var instanceType          = instance.GetType();
            var unproxyedInstanceType = unproxyedInstance.GetType();

            foreach (var property in unproxyedInstanceType
                     .GetRuntimeProperties()
                     .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (propertyType.GetTypeInfo().IsValueType&& !propertyType.GetTypeInfo().IsEnum)
                {
                    continue;
                }

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (property.GetIndexParameters().Length != 0)
                {
                    continue;
                }

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                var setParameter  = property.SetMethod.GetParameters().First();
                var valueProvider = (Func <object>)null;
                var parameter     = parameters.FirstOrDefault(p => p.CanSupplyValue(setParameter, context, out valueProvider));
                if (parameter != null)
                {
                    property.SetValue(unproxyedInstance, valueProvider(), null);
                    continue;
                }

                object propertyValue;
                var    propertyService       = new TypedService(propertyType);
                var    instanceTypeParameter = new NamedParameter(InstanceTypeNamedParameter, unproxyedInstanceType);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out propertyValue))
                {
                    property.SetValue(unproxyedInstance, propertyValue, null);
                }
            }
        }