Example #1
0
        private bool TryGetCustomConverter(Type type, out IJsonConverter customConverter)
        {
            if (!ConverterAttribute.IsDefined(type))
            {
                customConverter = null !;
                return(false);
            }

            var customConverterType = type
                                      .GetCustomAttribute <ConverterAttribute>()
                                      .GetConverterType(type);

            var injections = new LocalList <object>(type, this);

            customConverter = (IJsonConverter)_services.Activate(customConverterType, injections);
            return(true);
        }
Example #2
0
        public static Dictionary <string, IPropertyConverter <T> > ActivatePropertyConverters <T>(IServiceProvider services)
        {
            var ownerType  = Typeof <T> .Raw;
            var properties = ownerType.GetProperties();

            var propertyConverters = new Dictionary <string, IPropertyConverter <T> >(
                properties.Length,
                StringUtils.IgnoreCaseComparer);

            var converters = (IConvertersCollection)services.GetService(typeof(IConvertersCollection));

            foreach (var property in properties)
            {
                if (IgnoreAttribute.IsDefined(property))
                {
                    continue;
                }

                IPropertyConverter <T> propertyConverter;
                if (ConverterAttribute.IsDefined(property))
                {
                    var converterType = property
                                        .GetCustomAttribute <ConverterAttribute>()
                                        .GetConverterType(ownerType, property);

                    var injections = new LocalList <object>(property, ownerType);
                    propertyConverter = (IPropertyConverter <T>)services.Activate(converterType, injections);
                }
                else
                {
                    propertyConverter = new PropertyConverter <T>(property, converters.Get(property.PropertyType));
                }

                propertyConverters.Add(property.Name, propertyConverter);
            }

            return(propertyConverters);
        }
Example #3
0
        // The current impl. is super slow; use with caution.
        public static object Activate(this IServiceProvider services, Type type)
        {
            var ctors = type.GetConstructors()
                        .OrderByDescending(ci => ci.GetParameters().Length)
                        .ToList();

            var primaryCtor = ctors
                              .SingleOrDefault(ci => ci.GetCustomAttribute <ServiceConstructorAttribute>() != null);

            if (primaryCtor != null)
            {
                return(services.Activate(type, primaryCtor));
            }

            foreach (var ctor in ctors)
            {
                if (services.TryActivate(type, ctor, out var result))
                {
                    return(result);
                }
            }
            throw Errors.CannotActivate(type);
        }
        // Activate

        public static T Activate <T>(this IServiceProvider services, params object[] arguments)
        => (T)services.Activate(typeof(T), arguments);
 public static object GetOrActivate(this IServiceProvider services, Type type, params object[] arguments)
 => services.GetService(type) ?? services.Activate(type);
Example #6
0
 protected override object ResolveInstance(Type contract, IServiceProvider services)
 {
     return(services.Activate(Implementation, _constructor));
 }
Example #7
0
 public static T Activate <T>(this IServiceProvider services, ConstructorInfo constructorInfo)
     where T : class
 => (T)services.Activate(typeof(T), constructorInfo);
Example #8
0
        // Activate / TryActivate

        public static T Activate <T>(this IServiceProvider services)
            where T : class
        => (T)services.Activate(typeof(T));