示例#1
0
        public DependencyBinder(DependencyBindingOptions options, Type type)
        {
            Options = options;

            Type = type ?? throw new ArgumentNullException(nameof(type));

            binders = new List <IDependencyBinding>();
        }
示例#2
0
        public Kernel(DependencyBindingOptions bindingOptions = DependencyBindingOptions.ClassesInterfaces,
                      DependencyBindingOptions proxyOptions   = DependencyBindingOptions.ClassesInterfaces)
        {
            this.bindingOptions = bindingOptions;
            this.proxyOptions   = proxyOptions;

            binders      = new List <DependencyBinder>();
            dependencies = new List <Dependency>();
        }
示例#3
0
        public DependencyBinder(DependencyBindingOptions options, object instance)
        {
            this.instance = instance ?? throw new ArgumentNullException(nameof(instance));

            Options = options;
            Type    = instance.GetType();

            binders = new List <IDependencyBinding>();
        }
示例#4
0
        public static Type[] Map(Type type, DependencyBindingOptions options)
        {
            var types = new List <Type>();

            if ((options & DependencyBindingOptions.Classes) == DependencyBindingOptions.Classes && type.IsClass)
            {
                var it = type;

                while (it != null && it != typeof(object))
                {
                    types.Add(it);

                    it = it.BaseType;
                }
            }
            else if ((options & DependencyBindingOptions.Class) == DependencyBindingOptions.Class && type.IsClass)
            {
                types.Add(type);
            }
            else if ((options & DependencyBindingOptions.AbstractClasses) == DependencyBindingOptions.AbstractClasses)
            {
                var it = type;

                while (it != null && it != typeof(object))
                {
                    if (it.IsAbstract)
                    {
                        types.Add(it);
                    }

                    it = it.BaseType;
                }
            }

            if ((options & DependencyBindingOptions.Interfaces) == DependencyBindingOptions.Interfaces)
            {
                if (type.IsInterface)
                {
                    types.Add(type);
                }

                types.AddRange(type.GetInterfaces());
            }

            return(types.ToArray());
        }
示例#5
0
 public Kernel(DependencyBindingOptions bindingOptions)
     : this(bindingOptions, bindingOptions)
 {
 }
示例#6
0
        private DependencyBinder ConstructBinder(Type type, Type proxy, object instance, DependencyBindingOptions options)
        {
            if (type != null && instance != null)
            {
                throw new InvalidOperationException("both type and instance can't have a value");
            }

            DependencyBinder binder;

            if (instance != null)
            {
                binder = new DependencyBinder(options, instance);
            }
            else if (type != null)
            {
                binder = new DependencyBinder(options, type);
            }
            else
            {
                throw new InvalidOperationException("construction requires type of instance to continue");
            }

            var resolver = new DependencyBindingResolver(this);

            if (proxy != null)
            {
                binder.AsProxy(proxy);
            }

            if (instance == null)
            {
                if (!resolver.ResolveActivator(type, out var activator))
                {
                    throw new DependencyBinderException(type,
                                                        $"no activator for type {type.Name} could be created, please " +
                                                        $"check that the type has a public parameterless default " +
                                                        $"constructor or binding constructor available");
                }

                binder.BindWith(activator);
            }

            if (!resolver.ResolveBindings(type ?? instance?.GetType(), out var bindings))
            {
                return(binder);
            }

            binder.BindWith(bindings);

            return(binder);
        }