Ejemplo n.º 1
0
        /// <inheritdoc />
        public bool IsRegistered(Type type, string?name)
        {
            int hashCode = NamedType.GetHashCode(type, name);

            // Iterate through hierarchy and check if exists
            for (UnityContainer?container = this; null != container; container = container._parent)
            {
                // Skip to parent if no registry
                if (null == container._metadata || null == container._registry)
                {
                    continue;
                }

                // Look for exact match
                var registry     = container._registry;
                var targetBucket = (hashCode & HashMask) % registry.Buckets.Length;
                for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next)
                {
                    ref var candidate = ref registry.Entries[i];
                    if (candidate.HashCode != hashCode || candidate.Type != type ||
                        !(candidate.Value is ImplicitRegistration set) || set.Name != name)
                    {
                        continue;
                    }

                    return(true);
                }
            }
        internal object GetPolicy(Type type, string name, Type policyInterface)
        {
            var hashCode = NamedType.GetHashCode(type, name);

            // Iterate through containers hierarchy
            for (var container = this; null != container; container = container._parent)
            {
                // Skip to parent if no registrations
                if (null == container._registry)
                {
                    continue;
                }

                // Skip to parent if nothing found
                var registration = container._registry.Get(hashCode, type);
                if (null == registration)
                {
                    continue;
                }

                // Get the policy
                return(registration.Get(policyInterface));
            }

            return(null);
        }
Ejemplo n.º 3
0
 public PolicyKey(Type type, string name, Type policyType)
 {
     _policy = policyType;
     _type   = type;
     _name   = !string.IsNullOrEmpty(name) ? name : null;
     _hash   = NamedType.GetHashCode(type, name);
 }
Ejemplo n.º 4
0
 public HashKey(Type type, string?name)
 {
     _typeHash = type?.GetHashCode() ?? 0;
     _nameHash = name?.GetHashCode() ?? 0;
     HashCode  = NamedType.GetHashCode(_typeHash, _nameHash) & UnityContainer.HashMask;
     Type      = type;
     Name      = name;
 }
Ejemplo n.º 5
0
 public HashKey(string?name)
 {
     _typeHash = name?.Length ?? 0;
     _nameHash = name?.GetHashCode() ?? 0;
     HashCode  = NamedType.GetHashCode(_typeHash, _nameHash) & UnityContainer.HashMask;
     Type      = null;
     Name      = name;
 }
Ejemplo n.º 6
0
 public HashKey(Type type)
 {
     _typeHash = type?.GetHashCode() ?? 0;
     _nameHash = -1;
     HashCode  = NamedType.GetHashCode(_typeHash, _nameHash) & UnityContainer.HashMask;
     Type      = type;
     Name      = null;
 }
        internal IEnumerable <TElement> ComplexArray <TElement>(Func <Type, string, ImplicitRegistration, object> resolve, Type type)
        {
            TElement value;
            var      set      = new QuickSet <Type>();
            int      hashCode = type.GetHashCode();

            // Iterate over hierarchy
            for (var container = this; null != container; container = container._parent)
            {
                // Skip to parent if no data
                if (null == container._metadata)
                {
                    continue;
                }

                // Hold on to registries
                var registry = container._registry;

                // Get indexes and iterate over them
                var length = container._metadata.GetEntries(hashCode, type, out int[] data);
                for (var i = 1; i < length; i++)
                {
                    var index = data[i];
                    var name  = registry.Entries[index].Key.Name;
                    if (null == name)
                    {
                        continue;
                    }
                    if (set.Add(registry.Entries[index].HashCode, registry.Entries[index].Key.Type))
                    {
                        try
                        {
                            int hash         = NamedType.GetHashCode(typeof(TElement), name);
                            var registration = container.GetOrAdd(hash, typeof(TElement), name, registry.Entries[index].Value);
                            value = (TElement)resolve(typeof(TElement), name, registration);
                        }
                        catch (ArgumentException ex) when(ex.InnerException is TypeLoadException)
                        {
                            continue;
                        }

                        yield return(value);
                    }
                }
            }
        }
        /// <inheritdoc />
        public bool IsRegistered(Type type, string name)
        {
            int hashCode = NamedType.GetHashCode(type, name);

            // Iterate through hierarchy and check if exists
            for (var container = this; null != container; container = container._parent)
            {
                // Skip to parent if no registry
                if (null == container._metadata)
                {
                    continue;
                }

                // Look for exact match
                if (container._registry.Contains(hashCode, type))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        internal bool IsRegistered(ref BuilderContext context)
        {
            Type?generic = null;
            int  targetBucket, hashGeneric = -1;
            int  hashExact = NamedType.GetHashCode(context.Type, context.Name);

#if NETSTANDARD1_0 || NETCOREAPP1_0
            var info = context.Type.GetTypeInfo();
            if (info.IsGenericType)
            {
                generic     = info.GetGenericTypeDefinition();
                hashGeneric = NamedType.GetHashCode(generic, context.Name);
            }
#else
            if (context.Type.IsGenericType)
            {
                generic     = context.Type.GetGenericTypeDefinition();
                hashGeneric = NamedType.GetHashCode(generic, context.Name);
            }
#endif

            // Iterate through containers hierarchy
            for (UnityContainer?container = this; null != container; container = container._parent)
            {
                // Skip to parent if no registrations
                if (null == container._metadata || null == container._registry)
                {
                    continue;
                }

                var registry = container._registry;

                // Check for exact match
                targetBucket = (hashExact & HashMask) % registry.Buckets.Length;
                for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next)
                {
                    ref var candidate = ref registry.Entries[i];
                    if (candidate.HashCode != hashExact || candidate.Type != context.Type)
                    {
                        continue;
                    }

                    // Found a registration
                    return(true);
                }

                // Skip to parent if not generic
                if (null == generic)
                {
                    continue;
                }

                // Check for factory with same name
                targetBucket = (hashGeneric & HashMask) % registry.Buckets.Length;
                for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next)
                {
                    ref var candidate = ref registry.Entries[i];
                    if (candidate.HashCode != hashGeneric || candidate.Type != generic)
                    {
                        continue;
                    }

                    // Found a factory
                    return(true);
                }
        internal IEnumerable <TElement> ResolveEnumerable <TElement>(Func <Type, string, ImplicitRegistration, object> resolve,
                                                                     Type typeDefinition, string name)
        {
            TElement value;
            var      set         = new QuickSet <Type>();
            int      hashCode    = typeof(TElement).GetHashCode();
            int      hashGeneric = typeDefinition.GetHashCode();

            // Iterate over hierarchy
            for (var container = this; null != container; container = container._parent)
            {
                // Skip to parent if no data
                if (null == container._metadata)
                {
                    continue;
                }

                // Hold on to registries
                var registry = container._registry;

                // Get indexes for bound types and iterate over them
                var length = container._metadata.GetEntries <TElement>(hashCode, out int[] data);
                for (var i = 1; i < length; i++)
                {
                    var index = data[i];

                    if (set.Add(registry.Entries[index].HashCode, registry.Entries[index].Key.Type))
                    {
                        try
                        {
                            var registration = (ExplicitRegistration)registry.Entries[index].Value;
                            value = (TElement)resolve(typeof(TElement), registry.Entries[index].Key.Name, registration);
                        }
                        catch (ArgumentException ex) when(ex.InnerException is TypeLoadException)
                        {
                            continue;
                        }

                        yield return(value);
                    }
                }

                // Get indexes for unbound types and iterate over them
                length = container._metadata.GetEntries(hashGeneric, typeDefinition, out data);
                for (var i = 1; i < length; i++)
                {
                    var index = data[i];
                    var key   = registry.Entries[index].Key.Name;

                    if (set.Add(registry.Entries[index].HashCode, registry.Entries[index].Key.Type))
                    {
                        try
                        {
                            int hash         = NamedType.GetHashCode(typeof(TElement), key);
                            var registration = container.GetOrAdd(hash, typeof(TElement), key, registry.Entries[index].Value);
                            value = (TElement)resolve(typeof(TElement), key, registration);
                        }
                        catch (MakeGenericTypeFailedException) { continue; }
                        catch (InvalidOperationException ex) when(ex.InnerException is InvalidRegistrationException)
                        {
                            continue;
                        }
                        // TODO: Verify if required
                        //catch (ArgumentException ex) when (ex.InnerException is TypeLoadException)
                        //{
                        //    continue;
                        //}

                        yield return(value);
                    }
                }
            }

            // If nothing registered attempt to resolve the type
            if (0 == set.Count)
            {
                try
                {
                    var registration = GetRegistration(typeof(TElement), name);
                    value = (TElement)resolve(typeof(TElement), name, registration);
                }
                catch
                {
                    yield break;
                }

                yield return(value);
            }
        }
Ejemplo n.º 11
0
 public static int GetHashCode(Type type, string?name)
 => NamedType.GetHashCode(type, name) & UnityContainer.HashMask;
Ejemplo n.º 12
0
 public static int GetHashCode(Type type)
 => NamedType.GetHashCode(type.GetHashCode(), -1) & UnityContainer.HashMask;