Example #1
0
        public void RegisterByAttribute(Type type, InjectAttribute attribute)
        {
            var interfaces = new Type[] { };

            // Get as types
            if (attribute.InjectType == InjectType.AsInterface)
            {
                interfaces = type.GetInterfaces().Except(type.BaseType.GetInterfaces()).ToArray();

                if (interfaces.Length > 1)
                {
                    interfaces = interfaces.Except(interfaces.SelectMany(t => t.GetInterfaces())).ToArray();
                }

                if (interfaces.Length == 0)
                {
                    interfaces = type.BaseType.GetInterfaces();
                }
            }
            else if (attribute.InjectType == InjectType.AsSelf)
            {
                interfaces = new[] { type };
            }

            if (interfaces.Length == 0)
            {
                throw new Exception();
            }

            for (int index = 0; index < interfaces.Length; index++)
            {
                var interfaceType = interfaces[index];

                if (interfaceType.IsInterface && interfaceType.IsGenericType)
                {
                    interfaces[index] = interfaceType.GetGenericTypeDefinition();
                }
            }

            Register(type, interfaces, (int)attribute.LifeType);
        }
        public void RegisterByAttribute(Type type, InjectAttribute attribute)
        {
            var interfaces = new Type[] { };

            // Get as types
            if (attribute.InjectType == InjectType.AsInterface)
            {
                interfaces = type.GetInterfaces().Except(type.BaseType.GetInterfaces()).ToArray();

                if (interfaces.Length > 1)
                {
                    interfaces = interfaces.Except(interfaces.SelectMany(t => t.GetInterfaces())).ToArray();
                }

                if (interfaces.Length == 0)
                {
                    interfaces = type.BaseType.GetInterfaces();
                }
            }
            else if (attribute.InjectType == InjectType.AsSelf)
            {
                interfaces = new[] { type };
            }

            if (interfaces.Length == 0)
            {
                throw new Exception();
            }

            for (int index = 0; index < interfaces.Length; index++)
            {
                var interfaceType = interfaces[index];

                if (interfaceType.IsInterface && interfaceType.IsGenericType)
                {
                    interfaces[index] = interfaceType.GetGenericTypeDefinition();
                }
            }

            Register(type, interfaces, (int)attribute.LifeType);
        }