Ejemplo n.º 1
0
        public static void Register(this IRegisterableLake lake, Type t)
        {
            var attr = t.GetCustomAttribute <ComponentAttribute>();

            if (attr.Id != null)
            {
                if (attr.SingletonMode)
                {
                    lake.RegisterSingleton(attr.Id, () => new ObjectBuilder(t, lake).Build());
                }
                else
                {
                    lake.Register(attr.Id, () => new ObjectBuilder(t, lake).Build());
                }
            }
            else
            {
                if (attr.SingletonMode)
                {
                    lake.RegisterSingleton(attr.Type, () => new ObjectBuilder(t, lake).Build());
                }
                else
                {
                    lake.Register(attr.Type, () => new ObjectBuilder(t, lake).Build());
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 向一个湖注入一个已经被标记为组件的类
        /// </summary>
        /// <exception cref="ArgumentNullException">参数为空</exception>
        /// <exception cref="InvalidOperationException">传入的类型不是组件</exception>
        /// <param name="lake"></param>
        /// <param name="componentType"></param>
        public static void Register(this IRegisterableLake lake, Type componentType)
        {
            if (lake is null)
            {
                throw new ArgumentNullException(nameof(lake));
            }

            if (componentType is null)
            {
                throw new ArgumentNullException(nameof(componentType));
            }

            var componentAttribute = componentType.GetCustomAttribute <ComponentAttribute>();

            if (componentAttribute == null)
            {
                throw new InvalidOperationException($"{componentType.FullName} is not component");
            }
            if (componentAttribute.Id != null)
            {
                if (componentAttribute.SingletonMode)
                {
                    lake.RegisterSingleton(componentAttribute.Id, () => new ObjectBuilder(componentType, lake).Build());
                }
                else
                {
                    lake.Register(componentAttribute.Id, () => new ObjectBuilder(componentType, lake).Build());
                }
            }
            else
            {
                if (componentAttribute.SingletonMode)
                {
                    lake.RegisterSingleton(componentAttribute.Type, () => new ObjectBuilder(componentType, lake).Build());
                }
                else
                {
                    lake.Register(componentAttribute.Type, () => new ObjectBuilder(componentType, lake).Build());
                }
            }
        }
Ejemplo n.º 3
0
        private void Register(ComponentAttribute attr, MethodInfo method)
        {
            var methodProxy = new MethodProxy(instance, method, registerableLake);

            object factory()
            {
                try
                {
                    return(methodProxy.Invoke());
                }
                catch (ShouldAutoCreateException e)
                {
                    return(new ObjectBuilder(e.T, registerableLake).Build());
                }
            }

            if (attr.SingletonMode)
            {
                if (attr.Id == null)
                {
                    registerableLake.RegisterSingleton(method.ReturnType, factory);
                }
                else
                {
                    registerableLake.RegisterSingleton(attr.Id, factory);
                }
            }
            else
            {
                if (attr.Id == null)
                {
                    registerableLake.Register(method.ReturnType, factory);
                }
                else
                {
                    registerableLake.Register(attr.Id, factory);
                }
            }
        }