示例#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);
        }
示例#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);
        }
示例#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);
示例#6
0
 protected override object ResolveInstance(Type contract, IServiceProvider services)
 {
     return(services.Activate(Implementation, _constructor));
 }
示例#7
0
 public static T Activate <T>(this IServiceProvider services, ConstructorInfo constructorInfo)
     where T : class
 => (T)services.Activate(typeof(T), constructorInfo);
示例#8
0
        // Activate / TryActivate

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