示例#1
0
        internal static void AddRegistration(Registration dr)
        {
            if (StaticLogger.Level != StaticLogger.LoggingLevel.None)
            {
                StaticLogger.Log($"DI: Registering {dr.RegisteredType.FullName} => { dr.ToInstantiate.FullName} ({dr.ServiceProvider.Name})", StaticLogger.LoggingLevel.Call);
            }

            if (!Registrations.TryGetValue(dr.RegisteredType, out ConcurrentList <Registration> registrations))
            {
                registrations = new ConcurrentList <Registration>();
                Registrations.TryAdd(dr.RegisteredType, registrations);
            }

            ResolvableTypes.TryRemove(dr.RegisteredType, out _);

            registrations.Insert(0, dr);

            return;
        }
示例#2
0
        public void AddBinding(ServiceBinding serviceBinding)
        {
            SinglyLinkedListNode <Type>?currentNode = serviceBinding.ServiceTypes;

            while (currentNode != null)
            {
                Type type = currentNode.Value;
                if (!Registrations.TryGetValue(type, out Registration registration))
                {
                    registration = new Registration(serviceBinding);
                    Registrations.Add(type, registration);
                }
                else
                {
                    registration.AddBinding(serviceBinding);
                }

                currentNode = currentNode.Next;
            }
        }
        public void AddBinding(Binding binding)
        {
            SinglyLinkedListNode <Type>?currentNode = binding.BindingMetadata.DependencyTypes;

            while (currentNode != null)
            {
                Type type = currentNode.Value;
                if (!Registrations.TryGetValue(type, out Registration registration))
                {
                    registration = new Registration(type, binding);
                    Registrations.Add(type, registration);
                }
                else
                {
                    registration.AddBinding(binding);
                }

                currentNode = currentNode.Next;
            }
        }
示例#4
0
文件: Container.cs 项目: johans2/IoC
        /// <summary>
        /// Returns an object of type with all its constructor dependencies resolved.
        /// </summary>
        /// <param name="type">Type to resolve.</param>
        private object Resolve(Type type)
        {
            object instance;

            if (Instances.TryGetValue(type, out instance))
            {
                return(instance);
            }

            if (dependencyChain.Contains(type))
            {
                throw new CircularDependencyException("Circular dependency in " + type.ToString());
            }
            dependencyChain.Add(type);

            Type implementation;

            if (!Registrations.TryGetValue(type, out implementation))
            {
                throw new NullBindingException("Attempt to instanciate a null-binding. " + type.ToString() + " is not registered.");
            }

            ConstructorInfo constructor = GetConstructor(implementation);

            object[] dependencies = ResolvedDependencies(constructor);

            if (dependencies.Length < 1)
            {
                dependencyChain.Clear();
            }

            instance = constructor.Invoke(dependencies);
            Instances.Add(type, instance);

            return(instance);
        }
示例#5
0
 /// <summary>
 /// Try-Gets a list of registrations from the registration collection
 /// </summary>
 /// <param name="t">The type to check for</param>
 /// <param name="outT">If found, the return collection</param>
 /// <returns>Whether or not the type is registered as an injection target</returns>
 public static bool IsRegistered(Type t, out ConcurrentList <Registration> outT)
 {
     return(Registrations.TryGetValue(t, out outT));
 }